and_expression: equality_expression and_expression & equality_expression
For integral operands, the returned result is of type integer.
The and of two values is true only when both operands are true, as depicted in the truth table below.
P | Q | P&Q |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Note that the values assigned (top to bottom) to P and Q may be obtained by counting in binary (i.e. they are 0,1,2,3 in decimal).
The bitwise and operator applies the above truth table to successive bits in an integer as shown below:
Hex | f | f | f | f | ||||||||||||
Binary | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
&
Hex | 0 | f | 0 | f | ||||||||||||
Binary | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
==
Hex | 0 | f | 0 | f | ||||||||||||
Binary | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
Whilst the bitwise and operator is very useful for querying bits, the bitwise or operator is very useful for setting them.