Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3338014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:19:25+00:00 2026-05-18T00:19:25+00:00

I want to convert decimal numbers to binary numbers. I want to store them

  • 0

I want to convert decimal numbers to binary numbers. I want to store them in an array.
First I need to create an array that has a certain length so that I can store the binary numbers. After that I perform the conversion, here is how I do it:

public class Aufg3 {
    public static void main(String[] args) {
        int[] test = decToBin(12, getBinArray(12));
        for(int i = 0; i < test.length; i++){
            System.out.println(test[i]);
        }
    }

    public static int[] getBinArray(int number){
        int res = number, length = 0;
        while(res != 0){        
            res /= 2;
                    length++;
        }
        return new int[length];
    }

    public static int[] decToBin(int number, int[] array){
        int res = number, k = array.length-1;
        while(res != 0){
            if(res%2 == 0){
                array[k] = 0;
            }else{
                array[k] = 1;
            }
            k--;
            res /= 2;
        }
        return array;
    }
}

Is there anything to improve? It should print 1100 for input of 12.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:19:25+00:00Added an answer on May 18, 2026 at 12:19 am

    I assume you want to write your own code — otherwise this is straightforward to do using methods from the standard Java library.

    Some quick comments:

    • You can get rid of the res temp vars. Work directly on number (remember that Java passes parameters by value).
    • Shift is more efficient than division (number >>>= 1 instead of number /= 2), although the compiler should be able to optimize this anyway
    • You can avoid the modulus in decToBin if you just do array[k] = number & 1;
    • While you are at it, why not call getBinArray from decToBin directly? Then you can call decToBin with only one arg — the value to convert.

    Here is a slightly optimized version of your code:

    public static int[] getBinArray(int number) {
        int length = 0;
        while (number != 0) {
            number >>>= 1;
            length++;
        }
        return new int[length];
    }
    
    public static int[] decToBin(int number) {
        int[] array = getBinArray(number);
        int k = array.length-1;
        while (number != 0)
        {
            array[k--] = number & 1;
            number >>>= 1;
        }
        return array;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a column of numbers to numeric, but there are certain
I need to convert integers into two decimal float numbers. So if i have
I want to convert double precision numbers to decimal . for example I have
i want to convert decimal values to 16 bit binary values. i used this
Want to convert decimal to binary but am getting some bugs. I am now
I want to convert decimal number in binary number. I'm using this method: -
I want to convert a number to hex and store the result in a
I want to convert XML into binary data in Java? What is the fastest
I have the following problem: i need to convert a decimal number to string.
I want to convert a number to a 2 decimal places (Always show two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.