C - Bitwise Operators

C Programming

Share

In this tutorial we will learn about bitwise operators in C programming language.

Following are the bitwise operators that we can use in C to work with bits.

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR i.e. XOR
<<  Shift left
>>  Shift right

Don't apply bitwise operator to float and double.

Bitwise AND

Bitwise AND & will give 1 only if both the operands are 1 otherwise, 0.

The truth table for bitwise AND.

A B A & B
0 0 0
0 1 0
1 0 0
1 1 1

In the following example we have two integer values 1 and 2 and we will perform bitwise AND operation and display the result.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int
    x = 1,
    y = 2;

  //bitwise AND
  int result = x & y;
  
  printf("%d & %d = %d\n", x, y, result);
  
  return 0;
}

Output

1 & 2 = 0

Calculation of the bitwise AND operation for the above code.

  1 (decimal) = 0000 0001 (binary)
& 2 (decimal) = 0000 0010 (binary)
-----------------------------------
  0 (decimal) = 0000 0000 (binary)

Click here to learn how to convert decimal to binary.

Bitwise OR

Bitwise OR | will give 0 only if both the operands are 0 otherwise, 1.

The truth table for bitwise OR.

A B A | B
0 0 0
0 1 1
1 0 1
1 1 1

In the following example we have two integer values 1 and 2 and we will perform bitwise OR operation and display the result.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int
    x = 1,
    y = 2;

  //bitwise OR
  int result = x | y;
  
  printf("%d | %d = %d\n", x, y, result);
  
  return 0;
}

Output

1 | 2 = 3

Calculation of the bitwise OR operation for the above code.

  1 (decimal) = 0000 0001 (binary)
| 2 (decimal) = 0000 0010 (binary)
-----------------------------------
  3 (decimal) = 0000 0011 (binary)

Bitwise XOR

Bitwise XOR ^ will give 1 for odd number of 1s otherwise, 0.

The truth table for bitwise XOR.

A B A ^ B
0 0 0
0 1 1
1 0 1
1 1 0

In the following example we have two integer values 2 and 7 and we will perform bitwise XOR operation and display the result.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int
    x = 2,
    y = 7;

  //bitwise XOR
  int result = x ^ y;
  
  printf("%d ^ %d = %d\n", x, y, result);
  
  return 0;
}

Output

2 ^ 7 = 5

Calculation of the bitwise XOR operation for the above code.

  2 (decimal) = 0000 0010 (binary)
^ 7 (decimal) = 0000 0111 (binary)
-----------------------------------
  5 (decimal) = 0000 0101 (binary)

Shift Left

We use shift left << operator to shift the bits left.

In the following example we have an integer which we will left shift 1 position.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int x = 4;

  //shift left
  int result = x << 1;
  
  printf("Shift left x << 1 = %d\n", result);
  
  return 0;
}

Output

Shift left x << 1 = 8

Calculation:

4 (decimal) = 0000 0100 (binary)
----------------------------------
4 << 1 = 0000 1000 (binary)
       = 8 (decimal)

Shift Right

We use shift right >> operator to shift the bits right.

In the following example we have an integer which we will right shift 1 position.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int x = 4;

  //shift right
  int result = x >> 1;
  
  printf("Shift left x >> 1 = %d\n", result);
  
  return 0;
}

Output

Shift right x >> 1 = 2

Calculation:

4 (decimal) = 0000 0100 (binary)
----------------------------------
4 >> 1 = 0000 0010 (binary)
       = 2 (decimal)
Share