Results 1 to 4 of 4
 08132010, 06:25 AM #1Senior Member
 Join Date
 Mar 2009
 Posts
 126
 Rep Power
 0
help me in simple Bitwise operator :(
on the way in learning java i came across Bitwise operator and i don't understand how this Bitwise operator is really working , i mean please give me the real logic of the following program
Java Code:class BitOp { public static void main(String args[]){ int a=1; int b=2; int c=3; a=a 4; // Bitwise OR operator b>>=1; // Right shift assignment operator c<<=1; // Left shift assingment operator a=a^c; // Bitwise XOR operator System.out.println("a=" +a); System.out.println("b=" +b); System.out.println("c=" +c); } } result for the above example code is : a=3 b=1 c=6
please explain me the logic for :
1. Bitwise OR operator
2. Right shift operator
3 .Left shift operator
4. Bitwise XOR operator
of the above code ..........thanks in advance guys ..... keep rockingLast edited by funkygarzon; 08132010 at 09:14 AM. Reason: int c=3 has not declared
 08132010, 07:36 AM #2
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.
Imagine X = 8, Y = 6. Therefore, in binary, X == 1000 and Y == 110 (use a binary calculator, such as windows calculator, to calculate these if you need to; otherwise see my related links section for binary conversion lessons).
Now, we do this (line up the bits, note the added leading 0 on Y):
Java Code:1000 0110
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.
Java Code:0111 1010
Hope that's thorough enough. For more, here's related links:
Arithmetic shift  Wikipedia, the free encyclopedia
Bitwise operation  Wikipedia, the free encyclopedia
Hexadecimal, decimal and binary conversion chart.  AtariAge Forums
Binary Numbers  An intro to binary numbers & conversion formulas
Good luck!
 08132010, 08:21 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,393
 Blog Entries
 7
 Rep Power
 25
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
 08132010, 10:48 AM #4Senior Member
 Join Date
 Mar 2009
 Posts
 126
 Rep Power
 0
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 :)
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 :)
Similar Threads

How do i convert a int to a binary with bitwise operators only?
By diskhub in forum New To JavaReplies: 28Last Post: 05252010, 03:33 AM 
bitwise not ~ operator
By kezkez in forum New To JavaReplies: 2Last Post: 02102010, 07:49 AM 
Confusing with bitwise NOT operator
By Willi in forum New To JavaReplies: 4Last Post: 10162009, 11:06 PM 
Bitwise operations with long
By NewJavaBean in forum New To JavaReplies: 2Last Post: 04072009, 07:37 PM 
Using the bitwise operators
By Java Tip in forum java.langReplies: 0Last Post: 04172008, 07:39 PM
Bookmarks