Decimal binary conversion

I want to convert decimal numbers to binary numbers. I want to save them in an array. First I need to create an array with a specific length in order to store binary numbers. After that I do 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? He should type 1100 for input 12.

+4
source share
4 answers

I assume that you want to write your own code, otherwise it is easy to do this using methods from the standard Java library.

Some quick comments:

  • You can get rid of 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 module in decToBin if you just do array[k] = number & 1;
  • While you're on it, why not call getBinArray from decToBin directly? Then you can call decToBin with only one argument β€” 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; } 
+3
source

Why not just use the toBinaryString method of the Integer class:

 System.out.println(Integer.toBinaryString(12)) 
+6
source

If this is not homework, you do not need to do it yourself. The following code should work:

 BigInteger bigInt = new BigInteger(number); String asString = bigInt.toString(2); 

There may be more effective ways, but it is certainly very readable and supported.

+2
source

There are some little things you can improve:

  • You must define a "high level" method that converts int to int[] . In the current code, you should mention 12 two times, which is bad.
  • You should use the do { ... } while (number != 0) . Otherwise, the number 0 will be represented by an empty array.
  • You should use x >>> 1 instead of x / 2 , as this handles negative numbers correctly.
  • If you want to verify the correctness of the code, write another method that converts back from binary to int . Then you can check that binToDec(decToBin(12, ...)) == 12 .
  • The getBinArray method getBinArray not be public , as this is just a helper method. You can replace public with private or just remove public .
+1
source

All Articles