Alright, first off, the code above doesn't compile. The variable "c" is never declared.

But here's explanations of each of those things:

1. Bitwise OR (Inclusive Or)
Every number is represented as a string of 0s and 1s; 5 is 101, 7 is 111, 8 is 1000, and so on. When you do a bitwise IOR, for each bit (0 or 1), you keep it if the bit from the left is 1, or the bit from the right is 1, otherwise it's 0. Let me give an example.

Now since the first bit is 1 in X and 0 in Y, we keep it. The second bit is 1 in Y and 0 in X, so we keep it. Same with the third bit. The fourth bit, however, is 0 in both, so we do not keep it. We end up with the result 1110, which is 14. Therefore, 8 | 6 == 14.

2 and 3. Arithmetic shifts
When shifting, once again imagine bits. Imagine you have 10 (1010) as your number. When you left shift it, you add a 0 to the end (10100), which is 20. When you right shift it, you pop the last bit off the binary. 1010 becomes 101, which is 5. (Notice how LSH is just multiplying by 2, RSH is dividing by 2.)

4. Bitwise XOR (Exclusive Or)
This is just like IOR, except that ONLY ONE bit can be 1 to keep it.
Let's do 7 and 10.

Code:

0111
1010

The first bit is 1 in 10, 0 in 7, so we keep it. The second bit is 0 in 10, 1 in 7, so we keep it. The third bit, however, is 1 in both, so it is not kept. Fourth bit is the same as the second. Therefore, our result is 1101, which is 7 ^ 10 == 13.

Most, if not all of the Java textbooks describe bitwise operators in great detail, including examples and little tables. Why ask here? Reading doesn't hurt your eyeballs.

kind regards,

Jos

08-13-2010, 10:48 AM

funkygarzon

Quote:

Originally Posted by Zack

Alright, first off, the code above doesn't compile. The variable "c" is never declared.

But here's explanations of each of those things:

1. Bitwise OR (Inclusive Or)
Every number is represented as a string of 0s and 1s; 5 is 101, 7 is 111, 8 is 1000, and so on. When you do a bitwise IOR, for each bit (0 or 1), you keep it if the bit from the left is 1, or the bit from the right is 1, otherwise it's 0. Let me give an example.

Now since the first bit is 1 in X and 0 in Y, we keep it. The second bit is 1 in Y and 0 in X, so we keep it. Same with the third bit. The fourth bit, however, is 0 in both, so we do not keep it. We end up with the result 1110, which is 14. Therefore, 8 | 6 == 14.

2 and 3. Arithmetic shifts
When shifting, once again imagine bits. Imagine you have 10 (1010) as your number. When you left shift it, you add a 0 to the end (10100), which is 20. When you right shift it, you pop the last bit off the binary. 1010 becomes 101, which is 5. (Notice how LSH is just multiplying by 2, RSH is dividing by 2.)

4. Bitwise XOR (Exclusive Or)
This is just like IOR, except that ONLY ONE bit can be 1 to keep it.
Let's do 7 and 10.

Code:

0111
1010

The first bit is 1 in 10, 0 in 7, so we keep it. The second bit is 0 in 10, 1 in 7, so we keep it. The third bit, however, is 1 in both, so it is not kept. Fourth bit is the same as the second. Therefore, our result is 1101, which is 7 ^ 10 == 13.

thank you very much for your help my dear friend , i clearly understand the concept of IOR and XOR ,you are really rocking because you clearly understood where i am lacking ......... thank you again JACK :)

Quote:

Originally Posted by JosAH

Most, if not all of the Java textbooks describe bitwise operators in great detail, including examples and little tables. Why ask here? Reading doesn't hurt your eyeballs.

kind regards,

Jos

i am extremely sorry mate actually this program i have mentioned above is taken from one of the java course book which is available in our place , there they haven't mentioned clearly about the bitwise operator and also nothing striked my mind to google search for bitwise ,suddenly only our forum came to my mind for clearing my doubt regrading Bitwise operator .. so sorry thank you for your reply mate :)