C Programming: Using bitwise operators

‘C’ provides bitwise operators to deal with bits. Each bit is either “0” or “1”. In this article I am going to explain how to use bitwise operators in ‘C’.

‘C’ provides the following bitwise operators.

  • & (Ampersand – Bitwise AND Operator)
  • | (Pipe Symbol – Bitwise OR Operator)
  • ^ (Cap Symbol – Bitwise XOR Operator)
  • ~ (Tilde Symbol – Bitwise Complement or Bitwise NOT Operator)
  • << (2 ‘<‘ Symbols – Left Shift Operator)
  • >> (2 ‘>’ Symbols – Right Shift Operator)

To use these operators, we need two operands; except for bitwise NOT operator; it needs only one operand, that is why we can call this operator as an unary operator.

Lets discuss one-by-one here. I am going to give different examples for each one.

Bitwise AND Operator (“&”)

Compare each bit in two operands and returns “1”, when both the bits in two operands are “1”; otherwise it returns “0” for the particular bit position.

Eg: Lets take two values 123 and 87. Compare binary values between these two and apply bitwise AND operator (“&”). So the result will be 83. See the below calculation:

  0111 1011 (123)
& 0101 0111 (87)
= 0101 0011 (83)

Bitwise OR Operator (“|”)

Compares two operands and returns “0” when both the bits in two operands are “0”; otherwise it returns “1” for the particular bit position.

For example, see below code to understand how we use this operator in ‘C’.

int a = 123, b = 87;

c = a | b;

Bitwise Complement Operator (“~”)

It toggles each bit in the operand. Only one operand is required to use this operator; whereas other bitwise operators need two operands. This operator is called as an unary bitwise operator.

int a = 123;

a = ~a;

Bitwise XOR Operator (“^”)

This operator compares the bits between two operands and returns only “1” when either of the bit is “1”; otherwise it returns “0” for the particular bit position.

int onebit = 1, anotherbit = 0;

if ( onebit ^ anotherbit )
  printf("This is XOR.");

Test above condition by changing the values “0” or “1” in both the variables. And observe that, the condition will be true only when either one’s value is “1”; not for both.

Left Shift Operator (“<<“)

It shifts the bits left side in left side operand based on the value in right side operand. Same number of bits with value “0” will be added in right side.

This example left shifts the bits by 4 positions in the variable “a”.

int a = 123;

printf("%d", 123 << 4);

Right Shift Operator (“>>”)

This is useful to shift the bits right side in left side operand based on the value in right side operand. Same number of bits will be added from left side. The bit value is based on the sign of the number; if it is negative sign “1”s will be added; otherwise “0”s will be added.

Below example right shift 4 bits from the variable “a”.

int a = 123;

printf("%d", 123 >> 4);

Number of bits will be allocated based on the type of the variable and the Operating System you are using.

// Sample.c
#include <stdio.h>

void main()
{
   int a = 123, b = 87;

   printf("Bitwise AND (\"&\") : %d\n", a & b);
   printf("Bitwise OR (\"|\") : %d\n", a | b);
   printf("Bitwise XOR (\"^\") : %d\n", a ^ b);
   printf("Bitwise Complement (\"~\") : %d\n", ~a);
   printf("Bitwise Left Shift (\"<<\") : %d\n", a << 4);
   printf("Bitwise Right Shift (\">>\") : %d\n", a >> 4);
}

Run the above code and observe the results.

// Malin

Leave a Reply