# C - Bitwise AND operator

The Bitwise AND operator (&) is a binary operator which takes two bit patterns of equal length and performs the logical AND operation on each pair of corresponding bits. It returns 1 if both bits at the same position are 1, else returns 0.

Bit_1Bit_2Bit_1 & Bit_2
000
100
010
111

The example below describes how bitwise AND operator works:

```50 & 25 returns 16

50    ->    110010  (In Binary)
& 25    ->  & 011001  (In Binary)
----        --------
16    <-    010000  (In Binary)
```

The code of using Bitwise AND operator (&) is given below:

```#include <stdio.h>

int main (){
int x = 50;
int y = 25;
int z;

//Bitwise AND operation
z = x & y;

//Displaying the result
printf("z = %d\n", z);
return 0;
}
```

The output of the above code will be:

```z = 16
```

### Example: Check if a number is even or odd

The last bit of an even number is always 0, whereas for an odd number it bit is always 1. For a given number n, (n & 1) returns 0 if the n is even, else returns 1.

```Even Number:

50   ->    110010  (In Binary)
& 1   ->  & 000001  (In Binary)
----       --------
0   <-    000000

Odd Number:

99   ->    1100011  (In Binary)
& 1   ->  & 0000001  (In Binary)
----       ---------
1   <-    0000001
```

The below code checks whether a given number is even or odd:

```#include <stdio.h>

static void CheckEven(int MyNum){
if ((MyNum & 1) == 1){
printf("%d is an odd number.\n", MyNum);
} else {
printf("%d is an even number.\n", MyNum);
}
}

int main() {
CheckEven(50);
CheckEven(99);
return 0;
}
```

The above code will give the following output:

```50 is an even number.
99 is an odd number.
```

❮ C - Operators

5