## #1,107 – The Bitwise AND Operator

May 30, 2014 1 Comment

You can use the & operator to do a bitwise AND operation between two integer-based values.

An AND operation is applied to two different bits, returning true if both input bits are true. Here is a truth table showing the output value for all possible input combinations.

- 0 & 0 = 0
- 0 & 1 = 0
- 1 & 0 = 0
- 1 & 1 = 1

You can use the & operator on two arbitrary integer values as shown below. The AND operation will be applied to the two integer values, one bit at a time.

int n1 = 12; int n2 = 10; int result = n1 & n2; Console.WriteLine("{0} & {1} = {2}", n1, n2, result);

It may help to use hex notation in order to better understand how the AND operation works on each bit.

int n1 = 0xC; // 12 (dec) or 1100 (bin) int n2 = 0xA; // 10 (dec) or 1010 (bin) // result = 1000 int result = n1 & n2;