Results 1 to 4 of 4
  1. #1
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default 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
    NOw my request is :

    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 rocking
    Last edited by funkygarzon; 08-13-2010 at 10:14 AM. Reason: int c=3 has not declared

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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
    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.
    Java 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.

    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!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  4. #4
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    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
    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.
    Java 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.

    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!
    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 View Post
    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 :)

Similar Threads

  1. Replies: 28
    Last Post: 05-25-2010, 04:33 AM
  2. bitwise not ~ operator
    By kezkez in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 07:49 AM
  3. Confusing with bitwise NOT operator
    By Willi in forum New To Java
    Replies: 4
    Last Post: 10-17-2009, 12:06 AM
  4. Bitwise operations with long
    By NewJavaBean in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 08:37 PM
  5. Using the bitwise operators
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •