Understanding the bitwise AND Operator

Viewed 25533

I have been reading about bit operators in Objective-C in Kochan's book, "Programming in Objective-C".

I am VERY confused about this part, although I have really understood most everything else presented to me thus far.

Here is a quote from the book:

The Bitwise AND Operator

Bitwise ANDing is frequently used for masking operations. That is, this operator can be used easily to set specific bits of a data item to 0. For example, the statement

w3 = w1 & 3;

assigns to w3 the value of w1 bitwise ANDed with the constant 3. This has the same ffect of setting all the bits in w, other than the rightmost two bits to 0 and preserving the rightmost two bits from w1.

As with all binary arithmetic operators in C, the binary bit operators can also be used as assignment operators by adding an equal sign. The statement

word &= 15;

therefore performs the same function as the following:

word = word & 15;

Additionally, it has the effect of setting all but the rightmost four bits of word to 0. When using constants in performing bitwise operations, it is usually more convenient to express the constants in either octal or hexadecimal notation.

OK, so that is what I'm trying to understand. Now, I'm extremely confused with pretty much this entire concept and I am just looking for a little clarification if anyone is willing to help me out on that.

When the book references "setting all the bits" now, all of the bits.. What exactly is a bit. Isn't that just a 0 or 1 in 2nd base, in other words, binary?

If so, why, in the first example, are all of the bits except the "rightmost 2" to 0? Is it 2 because it's 3 - 1, taking 3 from our constant?

Thanks!

5 Answers

AND Operator (&) gives 1 only if both of the operands are 1, otherwise 0. By using AND (&) operator between given number (n) and 1, we are eliminating all bits other than LSB. After this operation, if the result is 0, that means LSB is 0 and the number is even. Otherwise, it is odd.

     0001 0001    (Binary of 17)                                                                                      
     0000 0001    (Binary of 1)                                                                                             
 & ------------------------------------                                                                                                    
    0000 0001 ------> 1, so 17 is odd number        

                                                                         

    0000 1000   (Binary of 8)                                                                                                               
    0000 0001   (Binary of 1)                                                                                                                 
 & ------------------------------------                                                                                                                            
    0000 0000 ------> 0, so 8 is even number     

if n is power of 2, by checking !(n & (n-1)).

Related