Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converstion from int to binary

Is there a way to convert an int to binary number without using Integer.toBinaryString method?
I tried to figure out the algorithm for the conversion but haven't had any luck with it.

My task is this: (https://open.kattis.com/problems/reversebinary)

Insert an Int with help of Scanner.
Convert the Int to binary.
Reverse the binary.
Print out the new Int.

For example, the number 11 is 1011 in binary.
Now reverse the binary number 1011 and you get 1101 (which is the number 13)
and print out 13.

This is what I got, but still, I used the Integer.toBinaryString method and I get NumberFormatException.

int reverse = 0;          
int number, binary;        

Scanner scn = new Scanner(System.in);          
number = scn.nextInt();         
String b = Integer.toString(number, 2);         
binary = Integer.parseInt(b);          

while (binary != 0) {          
    reverse = reverse * 10 + binary % 10;         
    binary = binary / 10;         

}
int newNumber = Integer.parseInt(String.valueOf(reverse), 2);        
System.out.println(newNumber);        

}
}
like image 245
DMT82 Avatar asked Feb 27 '26 21:02

DMT82


2 Answers

First of all, you should use the correct terms. You are not converting an int to a binary. The int type (as well as all numeric types) are already stored in a binary format. When you convert an int to a String, or a String to an int, you choose the radix that the String representation uses (such as decimal, binary, octal, hexadecimal, etc..). This determines the digits that appear in the String representation. Now, based on your example, what you wish to do is generate a number whose binary representation is the reverse of the input number. In other words, you want to reverse the bits of the input number.

Your current loop :

while (binary != 0) {          
    reverse = reverse * 10 + binary % 10;         
    binary = binary / 10;         
}

calculates the decimal (radix 10) digits of binary and creates an integer whose value is the value of those digits when they are arranged in reversed order.

If you want the reverse of the binary representation of the input number, you should multiply and divide by 2 in order to obtain the binary digits (aka bits) of the input number and reverse them :

while (number != 0) {     
    System.out.print (number % 2); // prints a binary digit (i.e. 0 or 1)    
    reverse = reverse * 2 + number % 2;         
    number = number / 2;         
}
System.out.println();
System.out.println(reverse); // prints the decimal representation of the reversed number

If number is 11, reverse will be 13, since the reverse of 1011 is 1101. This code will print both the binary representation of the reversed number (1101) and the decimal representation (13).

like image 96
Eran Avatar answered Mar 02 '26 10:03

Eran


Instead of reversing the binary as a number, reverse it while it is still a String, new StringBuilder(b).reverse().toString(). Then convert it back to an int from base 2, and you're done.

So the entire code would be:

    final Scanner scn = new Scanner(System.in);
    final int number = scn.nextInt();
    String b = Integer.toString(number, 2);
    b = new StringBuilder(b).reverse().toString();
    System.out.println(Integer.parseInt(b.toString(), 2));
like image 43
yedidyak Avatar answered Mar 02 '26 11:03

yedidyak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!