### What is Bitwise Operator

As a programmer we don’t have to worry about the operations at bit level. We have the flexibility to work in int, float, and even higher level data types. But being able to understand the bit level operation of a code will be highly significant for a programmer when he has to interact with the hardware or work at encryption, data compression etc.

Thanks to C which provides us with this capability as well, in the form of BITWISE Operators. Bitwise operators permit us to access and manipulate each bit in a given data. Following is the list of Bitwise Operators supported in C programming.

``````

&     Bitwise AND
|    Bitwise OR
~     Bitwise complement
^     Bitwise XOR (Exclusive OR)
<<     Left shift
>>     Right shift

``````

Lets work to understand each operator in detail with proper examples. But before starting remember that these operators can operate on only ints and chars but not floats and doubles.

## BITWISE AND ( & )

The ‘&’ operator operates on two operands of same type bit by bit. The rules that decide the value of each bit of result are as followed-

```		```

First operand     Second Operand    First Bit & Second Bit
0        0       0
0         1       0
1        0        0
1         1       1

```
```

### Example 1

Both the operands are positive

```		```

#include<stdio.h>
int main()
{
int i, j,k;
i=25;
j=37;
k=i&j;
printf("Output of Bitwise AND operation=%d",k);
return 0;
}

``````

OUTPUT-

Output of Bitwise AND operation=1

Explanation-

Variables of int type in C Language are of 2 bytes (16 bits) in size. So here we will understand the operation exactly at 16-bit level.

```		```

25= 0000 0000 0001 1001 (In Binary)
37= 0000 0000 0010 0101 (In Binary)

``````

Bitwise AND operation on 25 and 37

```		```

0000 0000 0001 1001
&	0000 0000 0010 0101
_____________________
0000 0000 000 00001 = 1 (In Decimal)
_____________________

``````

### Example 2

If one of the operand is negative

``````

#include<stdio.h>
int main()
{
int i, j,k;
i=-25;
j=37;
k=i&j;
printf(“Output of Bitwise AND operation=%d”,k);
return 0;
}

``````

. OUTPUT-

Output of Bitwise AND operation=37

Explanation-

To understand the operation with negative numbers we have to first understand the concept of 2’s complement. A negative number at bit level in computer is considered in 2’s complement form and then the desired operation is performed.

### 2’s complement

Positive Numbers

Positive 2's complement numbers are represented as the simple binary.

Negative Numbers

Negative 2's complement numbers are represented as the binary number that when added to a positive number of the same magnitude equals zero. To calculate the 2's complement of a negative integer

• Consider only the magnitude of the number (i.e ignore the negative sign)

• invert the binary equivalent of the magnitude by changing all of the ones to zeroes and all of the zeroes to ones (also called 1's complement)

Lets calculate 2’s complement of -25 -

• Consider only the magnitude

``````

25 = 0000 0000 0001 1001 (In Binary)
``````

• Invert all the bits-

``````

0000 0000 0001 1001 = 1111 1111 1110 0110 (Invert Bits, i.e. 1’s
complement)

``````

• Add 1 to 1’s complement

``````

1111 1111 1110 0110
+ 0000 0000 0000 0001
----------------------
1111 1111 1110 0111 (2’s complement of -25)
----------------------
Hence,

-25= 1111 1111 1110 0111 (In Binary, 2’s complement form)
37= 0000 0000 0010 0101 (In Binary)

Bitwise AND operation on 25 and 37
1111 1111 1110 0111
&	0000 0000 0010 0101
_____________________
0000 0000 0010 0101= 37 (In Decimal)
_____________________

``````

### Example 3

If both the operands are negative

``````

#include<stdio.h>
int main()
{
int i, j,k;
i=-25;
j=-37;
k=i&j;
printf(“Output of Bitwise AND operation=%d”,k);
return 0;
}

``````

OUTPUT-

Output of Bitwise AND operation=-61

Explanation

``````

-25= 1111 1111 1110 0111 (In Binary, 2’s complement form)
-37= 1111 1111 1101 1011 (In Binary, 2’s complement form)

Bitwise AND operation on 25 and 37
1111 1111 1110 0111
&	1111 1111 1101 1011
_____________________
1111 1111 1100 0011= -61 (2’s complement of -61)
_____________________

``````