11.2 The AND Operator ()

I l @ ve RuBoard

11.2 The AND Operator (&)

The AND operator compares two bits. If they both are 1, the result is 1. The results of the AND operator are defined in Table 11-3.

Table 11-3. AND operator

Bit1

Bit2

Bit1 & Bit2

1

1

1

1

1

When two 8-bit variables ( char variables) are "ANDed" together, the AND operator works on each bit independently. The following program segment illustrates this operation. (In the output statement below, hex tells the system to output numbers in hexadecimal format, and dec tells it to return to decimal. For more information, see Chapter 16.)

 int    c1, c2;  c1 = 0x45;  c2 = 0x71;  std::cout << "Result of " << hex << c1 << " & " << c2 << " = " <<                               (c1 & c2) << dec << '\n'; 

The output of this program is:

 Result of 45 & 71 = 41 

This is because:

 c1 = 0x45      binary 01000101 &  c2 = 0x71      binary 01110001 ___________________________________________ =  0x41           binary 01000001 

The bitwise AND ( & ) is similar to the logical AND ( && ). In the logical AND if both operands are true (nonzero), the result is true (1). In bitwise AND ( & ) , if the corresponding bits of both operands are true (1), the corresponding bits of the results are true (1). So the bitwise AND ( & ) works on each bit independently, while the logical AND ( && ) works on the operands as a whole.

However, & and && are different operators, as Example 11-1 illustrates.

Example 11-1. and/and.cpp
 #include <iostream> int main(  ) {     int i1, i2; // two random integers      i1 = 4;     i2 = 2;     // set values      // Nice way of writing the conditional      if ((i1 != 0) && (i2 != 0))         std::cout << "Both are not zero #1\n";     // Shorthand way of doing the same thing      // Correct C++ code, but rotten style      if (i1 && i2)         std::cout << "Both are not zero #2\n";     // Incorrect use of bitwise and resulting in an error      if (i1 & i2)         std::cout << "Both are not zero #3\n";     return (0); } 

Question: Why does test #3 fail to print Both are not zero #3 ?

Answer: The operator & is a bitwise AND. The result of the bitwise AND is zero:

 i1=4      00000100 &  i2=2      00000010 __________________________      0       00000000 

The result of the bitwise AND is 0, and the conditional is false. If the programmer had used the first form:

 if ((i1 != 0) && (i2 != 0)) 

and made the mistake of using & instead of && :

 if ((i1 != 0) & (i2 != 0)) 

the program would still have executed correctly.

 (i1 != 0)       is true (result = 1) (i2 != 0)       is true (result = 1) 

1 bitwise AND 1 is 1, so the expression is true.

Soon after discovering the bug illustrated by this program, I told my officemate, "I now understand the difference between AND and AND AND ), and he understood me. How we understand language has always fascinated me, and the fact that I could utter such a sentence and have someone understand it without trouble amazed me.

You can use the bitwise AND operator to test whether a number is even or odd. In base 2, the last digit of all even numbers is zero and the last digit of all odd numbers is one. The following function uses the bitwise AND to pick off this last digit. If it is zero (an even number), the result of the function is true.

 inline int even(const int value) {     return ((value & 1) == 0); } 
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net