Bitwise Operators in C



The bitwise operators in C allow a low-level manipulation of data stored in computer’s memory. The bitwise operators contrast with logical operators in C, which perform variable level operations. For example, the logical AND operator (represented by && symbol) performs AND operation on two Boolean expressions, the bitwise AND operator (represented by & symbol) performs the AND operation on each corresponding bit of the two operands.

For the three logical operators &&, || and !, the corresponding bitwise operators in C are &, | and ~. Additionally, the symbols ^ (XOR), << (left shift) and >> (right shift) are the other bitwise operators.

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B)
| Binary OR Operator copies a bit if it exists in either operand. (A | B)
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B)
~ Binary One's Complement Operator is unary and has the effect of 'flipping' bits. (~A
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2

Even though these operators work on individual bits, they need the operands in the form C data types or variables only, as a variable occupies a specific number of bytes in the memory.

& operator

The bitwise AND (&) operator performs as per the following truth table −

bit a bit b a & b
0 0 0
0 1 0
1 0 0
1 1 1

Bitwise binary AND performs logical operation on the bits in each position of a number in its binary form.

Assuming that the two int variables a and b have the values 60 (equivalent to 0011 1100 in binary) and 13 (equivalent to 0000 1101 in binary), the a&b operation results in 13, as per the bitwise ANDing of their corresponding bits illustrated below −

  0011 1100  
& 0000 1101 
  --------- 
= 0000 1100

The binary number 00001100 corresponds to 12 in decimal.

| operator

The bitwise OR (|) operator performs as per the following truth table −

bit a bit b a | b
0 0 0
0 1 1
1 0 1
1 1 1

Bitwise binary OR performs logical operation on the bits in each position of a number in its binary form.

Assuming that the two int variables a and b have the values 60 (equivalent to 0011 1100 in binary) and 13 (equivalent to 0000 1101 in binary), the a|b operation results in 61, as per the bitwise OR of their corresponding bits illustrated below −

  0011 1100   
| 0000 1101  
  --------- 
= 0011 1101

The binary number 00111101 corresponds to 61 in decimal.

^ operator

The bitwise XOR (^) operator performs as per the following truth table −

bit a bit b a ^ b
0 0 0
0 1 1
1 0 1
1 1 0

Bitwise binary XOR performs logical operation on the bits in each position of a number in its binary form. The XOR operation is called exclusive OR. The result of XOR is 1 if and only if one of the operands is one. Unlike OR, if both bits are 1, XOR results in 0.

Assuming that the two int variables a and b have the values 60 (equivalent to 0011 1100 in binary) and 13 (equivalent to 0000 1101 in binary), the a^b operation results in 49, as per the bitwise XOR of their corresponding bits illustrated below −

  0011 1100   
^ 0000 1101  
  --------- 
= 0011 0001

The binary number 00110001 corresponds to 49 in decimal.

<< operator

The left shift operator is represented by the << symbol and shifts each bit in its left-hand operand to the left by the number of positions indicated by the right-hand operand. Any blank spaces generated while shifting are filled up by zeroes.

Assuming that the int variable a has the value 60 (equivalent to 0011 1100 in binary), the a<<2 operation results in 240, as per the bitwise left-shift of its corresponding bits illustrated below −

0011 1100 << 2 = 1100 0100

The binary number 00111100 corresponds to 240 in decimal.

>> operator

The right shift operator is represented by the >> symbol and shifts each bit in its left-hand operand to the right by the number of positions indicated by the right-hand operand. Any blank spaces generated while shifting are filled up by zeroes.

Assuming that the int variable a has the value 60 (equivalent to 0011 1100 in binary), the a>>2 operation results in 15, as per the bitwise right-shift of its corresponding bits illustrated below −

0011 1100 >> 2 = 0000 1111

The binary number 00001111 corresponds to 15 in decimal.

~ operator

The ~ symbol is defined as One’s compliment operator in C. It is a unary operator, needing just one operand. It has the effect of 'flipping' bits, which means that 1 is replaced by 0, and 0 by 1 in the binary representation of any number.

a ~a
0 1
1 0

Assuming that the int variable a has the value 60 (equivalent to 0011 1100 in binary), the ~a operation results in -61 in 2’s complement form, as per the bitwise right-shift of its corresponding bits illustrated below −

~ 0011 1100 = 1100 0011

The binary number 1100 0011 corresponds to -61 in decimal.

Example

#include <stdio.h>

int main() {

   unsigned int a = 60;	/* 60 = 0011 1100 */  
   unsigned int b = 13;	/* 13 = 0000 1101 */
   int c = 0;           

   c = a & b;       /* 12 = 0000 1100 */ 
   printf("Line 1 - Value of c is %d\n", c );

   c = a | b;       /* 61 = 0011 1101 */
   printf("Line 2 - Value of c is %d\n", c );

   c = a ^ b;       /* 49 = 0011 0001 */
   printf("Line 3 - Value of c is %d\n", c );

   c = ~a;          /*-61 = 1100 0011 */
   printf("Line 4 - Value of c is %d\n", c );

   c = a << 2;     /* 240 = 1111 0000 */
   printf("Line 5 - Value of c is %d\n", c );

   c = a >> 2;     /* 15 = 0000 1111 */
   printf("Line 6 - Value of c is %d\n", c );
   
   return 0;
}

Output

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
c_operators.htm
Advertisements