permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Binary representation of an int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Binary representation of an int" Watch "Binary representation of an int" New topic
Author

Binary representation of an int

Gary Busby
Greenhorn

Joined: Feb 02, 2003
Posts: 5
I have been doing some exercises in Mugal and Rasmussen's book. The exercise asks to "Create a method that takes an int as an argument and returns a String objectrepresenting the binary representation of the integer. Given the argument 42, it should return "101010"."
I had a go but came no-where close. The answer in the back of the book has me speechless. I had placed some System.out.println's to see how the program works and then reffered to the book to work it out but I can't see how it works.
Could someone explain how this program works between the From here to Here markers?
public class scjpex_32 {
public static void main(String args[]) {
System.out.println(toBinary(42));
}

public static String toBinary(int i) {
String binary = "";
do {
// From here
int lowBit = (i&1);
String newDigit = ((lowBit == 0) ? "0" : "1");
binary = newDigit + binary;
// To here
i>>>= 1;
} while (i !=0);
return binary;
}
}
[ October 09, 2003: Message edited by: Gary Busby ]
[ October 09, 2003: Message edited by: Gary Busby ]
[ October 09, 2003: Message edited by: Gary Busby ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Gary Busby:
I have been doing some exercises in Mugal and Rasmussen's book. The exercise asks to "Create a method that takes an int as an argument and returns a String objectrepresenting the binary representation of the integer. Given the argument 42, it should return "101010"."
I had a go but came no-where close. The answer in the back of the book has me speechless. I had placed some System.out.println's to see how the program works and then reffered to the book to work it out but I can't see how it works.
Could someone explain how this program works between the From here to Here markers?
public class scjpex_32 {
public static void main(String args[]) {
System.out.println(toBinary(42));
}

public static String toBinary(int i) {
String binary = "";
do {
// From here
int lowBit = (i&1);
String newDigit = ((lowBit == 0) ? "0" : "1");
binary = newDigit + binary;
// To here
i>>>= 1;
} while (i !=0);
return binary;
}
}
[ October 09, 2003: Message edited by: Gary Busby ]
[ October 09, 2003: Message edited by: Gary Busby ]

Welcome to the world of bitwise operators, Gary. You'll either love it or hate it here.
Okay, let's take a look at this application and see what's going on, shall we?
We start the the decimal number 42, which is 101010 in binary.

This line is using the bitwise 'AND' operator to strip away everything except the least significant bit. Basically, we're doing this:

This is what is known as "bit masking" and the most essential thing to understand in order to understand this application. We're using the bitwise AND operator to strip away all of the other bits. If you don't understand this, think about what the bitwise AND does. If you AND something with 1, what do you get? You get whatever you started with. Basically, lowBit is going to have a value of 0 or 1, depending upon what the least significant bit was in the original bumber. In this case, it's 0.

These lines certainly aren't rocket-science. We're simply checking to see if lowBit was a 0 or a 1. In either case, we add the corresponding String to the total String. So, for the first case, we had a '0', so we add "0" to "" and end up with "0."

Now that we're done with the least significant bit, we need to get ready for the next iteration of the loop. We do this by shifting everything to the right one space. canges i to:

Now, we simply go back through the loop using this new value for i. The bitmask returns a 1 this time (the least significant bit is now a 1) and we add the String representation of that to the current string, which gives us "10." As you can see, we're on our way. We just need to keep iterating over that loop until we're done.
This can be quite confusing at first. I've tried to explain it but, if I'd confused you, just let me know and I'll try to iron out the confusing parts.
Corey


SCJP Tipline, etc.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
If we could use API then this is very simple
String s = Integer.toBinaryString(input);
and we are done.
However, if we do not use API or any of the bit operators such as &, this could be difficult. Also, if you consider a negative, it could be more complicated. Just to practice programming, I wrote following:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Binary representation of an int