how is line 2 & 3 getting the second and first bit?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • futSecGuy1990
    New Member
    • Apr 2015
    • 15

    how is line 2 & 3 getting the second and first bit?

    Can some one explain how it's get the first and second bit, so, how is (i&2)/2 getting the second bit ?

    Code:
    for(i=0; i < 4; i++) {     
     bit_a = (i & 2) / 2; // Get the second bit.     
     bit_b = (i & 1);     // Get the first bit.      
    printf("%d | %d = %d\n", bit_a, bit_b, bit_a | bit_b);  
     }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    OK.

    Let's assume i is 8 bits to keep things simple. Let's also assume i has a value of 245 which is 11110101 in binary.

    The bits are numbered 0 to 7 from right to left:

    11110101
    ..........^.... ... bit 2

    Now you can AND this number with 00000100. This is called a mask.

    11110101
    00000100
    --------------- AND
    00000100

    So the result of the AND will have a 1 when the number AND the mask both have a 1 in the same bit position.

    In this case the result of the AND is equal to the mask. This means that bit 2 is 1 in the variable i.

    Code:
    unsigned int i = 245;
    unsigned int mask = 4;
    unsigned int result;
    
    result = i & mask;   //The & is the bitwise AND operator
    
    if (result == mask)
    {
        //bit 2 is ON
    }

    Comment

    • futSecGuy1990
      New Member
      • Apr 2015
      • 15

      #3
      Ok, im still a little confused tho, sorry if this is frustrating For you!!!
      Code:
       bit_a = (i & 2) / 2; // Get the second bit.
      so keeping things simple. This is comparing
      0000 0000
      &
      0000 0010 and the / 2 is saying look at the 2ND bit?

      Comment

      • futSecGuy1990
        New Member
        • Apr 2015
        • 15

        #4
        Sorry I posted a reply but did not post to your answer. Could you check my reply out and see if I'm understanding this correctly

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          In this code:

          Code:
          bit_a = (i & 2) / 2; // Get the second bit.
          it says get the second bit. 2 is 00000010. The 1 is in bit 1, not bit 2. It is bit 1 because the position of the bit is 2 to the 1st power.

          The bit on the far right is bit 0. To the left of bit 0 is bit 1.

          The bit is either ON (1) or OFF(0).


          Now look at i & 2:


          Let's look at the possible conditions

          00000010

          or

          00000000

          If you AND with 2 (00000010) you get:

          00000010
          00000010
          ............... ..AND
          00000010

          or you get

          00000000
          00000010
          ............... ..AND
          00000000


          In the first case the result of the AND is 00000010, which is 2. i & 2 becomes 2. So now you take 2 / 2 which is 1. Hence the bit is ON.

          The other case is 00000000 which is 0. Then 0/2 is 0. Hence the bit is OFF.

          Comment

          • futSecGuy1990
            New Member
            • Apr 2015
            • 15

            #6
            Ok so, let's say i =3

            Bit_a
            0000 0011
            &
            0000 0010
            ------------
            0000 0010 = (2)/2 = 1
            So on the final iteration
            bit_a = 1

            Bit_b
            0000 0011
            &
            0000 0001
            -----------
            0000 0001 = 1
            Bit_b = 1

            And then in the print statement compares bit_a , bit_b using the OR operator

            So...
            0000 0001 bit_a
            or
            0000 0001 bit_b
            ------------
            0000 0001 so the result of the printf in the final iteration should be 1 ?

            Someone on another forum, as far as I understood it, was saying that the /2 is a different form of the shift operator, but it's really just the result of AND divided by 2. And the mask in this code would be the 2 for bit_a and 1 for bit_b. And the mask is used to look at a specific portion of 'i'. So in this case I'm looking at bit1 for the variable bit_a and bit0 for the variable bit_b.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Yes, /2 is not the shift operator. It is the division operator.

              The shift operator is << or >> to shift bits left or right.

              As to your analysis on bit_a or bit_b. it looks correct. Congratulations .

              So far you have been testing bits. Using similar approaches, you can set or reset bits.

              Comment

              Working...