6 Replies Latest reply: Dec 7, 2008 5:59 PM by 807589

# Bitwise Operations

Any help will be appreciated!!

Say I have an int thats 32 bit single flags how do you set bits 0 and 9 and/or clear bits 3 and 10?

Thanks a lot
• ###### 1. Re: Bitwise Operations
[http://www.javaranch.com/campfire/StoryBits.jsp]

~
• ###### 2. Re: Bitwise Operations
You do know bits are just binary number representations and can be easily and simply manipulated with binary math. I, and many others, prefer to use hex notation for binary manipulation--each 4 bits, a nibble, is represented by each digit of a hex number. The "&" operator does the standard "and" function and the "|" operator does the standard or function bit by bit.

bit 0: 0x00000001 used with an Or to set the bit (These patterns are called masks)
bit 0: 0xFFFFFFFFE used with an AND to clear the bit

The same functionality can be used across each bit of your int by changing the location of the 1 or 0 to set or clear the desired bit.
``````//If I want to clear the 9th bit of this number: 0x00000100
int myInt = 0x00000100;
myInt = myInt & FFFFFEFF; //this will result in myInt becoming 0.
//if I want to set the 2nd bit of myInt
myInt = myInt | 0xFFFFFFF2; //will result in myInt being set to 2 base 10.``````
Notice these functions are equivalent to subtracting and adding the appropriate binary number if, and only if, the appropriate bit can be guaranteed set or clear before the start of the targeted by the operation. You can clear the 9th bit by subtracting 0x00000100 from any number that has the 9th bit set. you can set the second bit by adding 2 to any number that has the 2nd bit clear to start with.
• ###### 3. Re: Bitwise Operations
yawmark wrote:
[http://www.javaranch.com/campfire/StoryBits.jsp]

~
I appreciate!
• ###### 4. Re: Bitwise Operations
morgalr wrote:
You do know bits are just binary number representations and can be easily and simply manipulated with binary math. I, and many others, prefer to use hex notation for binary manipulation--each 4 bits, a nibble, is represented by each digit of a hex number. The "&" operator does the standard "and" function and the "|" operator does the standard or function bit by bit.

bit 0: 0x00000001 used with an Or to set the bit (These patterns are called masks)
bit 0: 0xFFFFFFFFE used with an AND to clear the bit

The same functionality can be used across each bit of your int by changing the location of the 1 or 0 to set or clear the desired bit.
``````//If I want to clear the 9th bit of this number: 0x00000100
int myInt = 0x00000100;
myInt = myInt & FFFFFEFF; //this will result in myInt becoming 0.
//if I want to set the 2nd bit of myInt
myInt = myInt | 0xFFFFFFF2; //will result in myInt being set to 2 base 10.``````
Notice these functions are equivalent to subtracting and adding the appropriate binary number if, and only if, the appropriate bit can be guaranteed set or clear before the start of the targeted by the operation. You can clear the 9th bit by subtracting 0x00000100 from any number that has the 9th bit set. you can set the second bit by adding 2 to any number that has the 2nd bit clear to start with.
Thanks a lot this was helpful but I'm still a little confused.

What is the 9th bit of this number 0x00000100 ? This looks like it only has 8 bits...

And how will I set the 3rd or 4th bit of myInt in your example.

I'm just trying to understand how you are doing it cause its still not clear

Thanks
• ###### 5. Re: Bitwise Operations
kci wrote:
Thanks a lot this was helpful but I'm still a little confused.

What is the 9th bit of this number 0x00000100 ? This looks like it only has 8 bits...
It's 8 nibbles, which is 4 bytes, which is 32 bits, as he explained in his post.
``````0x      12         34          56          78
is
0001 0010   0011 0100   0101 0110   0111 1110``````
And how will I set the 3rd or 4th bit of myInt in your example.
I'm pretty sure this is covered in the tutorial he linked, or in any of the copious examples you could find on google.

This forum is not a substitute for time spend reading and playing with code. After reading the tutorial and going through his example code, and making changes and observing what happens, what in particular do you not understand?
• ###### 6. Re: Bitwise Operations
I probably should have said this earlier:

bit images for 0x00000100 are as follows:
0000 0000 0000 0000 0000 0001 0000 0000
Each digit is a 4 bit combination called a nibble--1/2 byte--of memory. and in HEX they are addressed from 0 through F inclusive (0 to 15 decimal):
hex 0 binary 0000 decimal 0
hex 1 binary 0001 decimal 1
hex 2 binary 0010 decimal 2
hex 3 binary 0011 decimal 3
hex 4 binary 0100 decimal 4
hex 5 binary 0101 decimal 5
hex 6 binary 0110 decimal 6
hex 7 binary 0111 decimal 7
hex 8 binary 1000 decimal 8
hex 9 binary 1001 decimal 9
hex A binary 1010 decimal 10
hex B binary 1011 decimal 11
hex C binary 1100 decimal 12
hex D binary 1101 decimal 13
hex E binary 1110 decimal 14
hex F binary 1111 decimal 15

To make a mask to remove bits you make the binary representation:

lets say 10 decimal we want to mask out the 3rd bit.
10 is 0110 in binary and the bit what we want to manipulate is 0100 with our "AND" so we do a 1's compliment of our binary number representation of the bit we want manipulated (0100) and this will produce what we call a mask 1011. Now you use a bit wise AND.

Remember the logic diagram for AND
0 * 0 = 0
1 * 0 = 0
0 * 1 = 0
1 * 1 = 1

So 0110 & 1011 gives 0010 which is 2--the 3rd bit has just been "masked off".

Mask operate the same no matter the size, so pic the bit you want and mask away!