File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes bit operations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "bit operations" Watch "bit operations" New topic

bit operations

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
I was inspired to solve the problem posted by someone recently about bit operations. However, I am afraid to ask for too much help because I really don't want to just post the answer out here for that person to steal. So I am going to try to ask for very specific help on a couple things I'm not exactly sure on.
How many bits are in an int? I have written a method to print out 32 characters, either 1 or 0, based on the int value you pass in.

(I don't want to give away the code ) anyway, my for loop starts at 0, and loops until i < 31. That seems to work fine (when I start my bit mask at 0x40000000), but I thought I should have been going until 32. I think it has something to do with having the one bit representing the sign, but the behavior of the method changes completely when I just loop until 32 and start my bit mask at 0x80000000. Basically, if the mask starts at 0x40000000 and loops 31 times, the bit representation that prints out is correct. But if the mask starts at 0x80000000 and I loop 32 times, the output is all 0's. I was expecting to see the correct bit pattern again, and if the number was negative, the bit to the very left (I guess they'd call it bit 31) would be 1 instead of 0.
I hope I have explained what I'm trying to say... if anybody has any input I'd appreciate it.

Studying for SCJP 6
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
I tried writing that code, and the thing to remember about the mask 0x80000000 is that as an int that 1 means it is a negative number.

When you shift the bit in the mask, make sure you use the unsigned right shift operator.

Also don't loop until i < 31. That will only give you up to 30.
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Thanks for the reply. I did get it working, the reverse and everything. I had to alter my approach a little though. I started the bit mask at 1, and shifted left. Then I just had to reverse the string.
What do you mean by "make sure you use the unsigned right shift operator?" Is there a different right shift operator that is signed? I only ask because this is what I observed: when starting the mask at 0x80000000, that is (I believe) the largest negative value for type int. However, when shifting to the right, the next number was still negative. So that means doing a right bit shift did NOT change the mask to 0x40000000 like I was expecting. I did something like:

is there a different way of shifting the bit to the right?
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
Yes there are two shift-operators to the right.
One that lets the first bit (bit31) untouched and whether it was set or not will insert a1 from the left. That`s the >>-operator.
100000...0 >> 2 should result in 101000...0

The one you were looking for was probably the >>>-operator
100000..0 >>> 2 results in 0010000...0

Btw: You know about the toBinaryString-method in class Integer?

static void showIntAsBit(int i){
System.out.println(i + " = " + Integer.toBinaryString(i));
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Hey Sasha, thanks for clearing that up for me. You're right, I was trying to use >> because I have never seen >>> before. And no, I did not know about the toBinaryString() method, although I did enjoy the challenge of recreating it. Besides, I can format my binary string how I want, and I happen to like having a space every 8 bits. Anyway, it's good to know about the >>> operator.
[ March 31, 2007: Message edited by: Eric Daly ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
couldn't you just use String t = Integer.toBinaryString(int a); and just compare
the bit mask and the new string representation of the num.

and then just say.

t += "0";
t += "1";




You down with OOP? Yeah you know me!
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
I'm not sure I understand what you mean exactly. Once you declare t as the binary string representation of the integer, why would you compare the bit mask and continue to add "1" or "0" to the string? You've already got your string. Although, that doesn't print out leading 0's, and as far as I know, you can't do any formatting with toBinary(int num). Like I said earlier, it was a good challenge (and good practice) to write the function myself, plus I have the fringe benefit of being able to format the string however I want (which includes printing leading 0's).
Another thing is that toBinary(int num) does not modify num. The real problem was reversing the bits in num and actually updating num (not just printing the string in reverse). Anyway, could you be more clear on what you mean?
couldn't you just use String t = Integer.toBinaryString(int a); and just compare
the bit mask and the new string representation of the num.

I can't imagine trying to compare a bit mask to a string.
I agree. Here's the link:
subject: bit operations
It's not a secret anymore!