The leftmost column shows all values for P, the middle column all values for Q, and the rightmost column the results of the AND operation on the two inputs.

P AND (&) Q | ||
---|---|---|

P | Q | P AND Q |

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

Now, how do we translate this operation to actual integer values which can range far beyond the values contained in a single bit (as portrayed in this truth table)?. It's quite easy actually, let's take a look at some 8-bit (1 byte) values:

We take 2 random values, let's say 0xF8 (248 decimal) and 0x15 (21 decimal). Let's lay these values out in binary form:

**0xF8:**1 1 1 1 1 0 0 0

**0x15:**0 0 0 1 0 1 0 1

When we look at these numbers in a binary form it gets much easier to do a logical AND operation on them. We iterate over the presented binary numbers in the first integer and we do a logical AND with the matching binary value in the other integer.

Let's have a look at the result:

1 1 1 1 1 0 0 0

0 0 0 1 0 1 0 1 (AND)

-------------------

0 0 0 1 0 0 0 0

If we put the resulting binary number back into a hexadecimal form we get 0x10 (16 decimal).

So when you use the &-operator in C++ on the values 0xF8 and 0x15 you would get 0x10 as a result (0xF8 & 0x15 == 0x10)

EDIT:

Wow, it seems like I take a very long time to post