Results 1 to 4 of 4
- 08-13-2010, 06:25 AM #1
Senior 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
NOw my request is :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; 08-13-2010 at 09:14 AM. Reason: int c=3 has not declared
- 08-13-2010, 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):
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.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.
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.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!
- 08-13-2010, 08:21 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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 #4
Senior 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: 05-25-2010, 03:33 AM -
bitwise not ~ operator
By kezkez in forum New To JavaReplies: 2Last Post: 02-10-2010, 06:49 AM -
Confusing with bitwise NOT operator
By Willi in forum New To JavaReplies: 4Last Post: 10-16-2009, 11:06 PM -
Bitwise operations with long
By NewJavaBean in forum New To JavaReplies: 2Last Post: 04-07-2009, 07:37 PM -
Using the bitwise operators
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks