Introducing with bitwise operators in C

Bitwise operators can work with a data at bit level. Programming with bitwise operator is known as bit level programming.

We know that 0 and 1 are called binary digit or bit. So, bit level programming consists of these two digits 0 and 1. This can make the program faster and for this bit level programming is used in numerical computations.

There are mainly six bitwise operators in C programming. These are given in the table below;

List of six bitwise operators in C

Name of operator Symbol
Bitwise AND operator
&
Bitwise OR operator
|
Bitwise EX-OR operator
^
One's complement operator
~
Right shift operator
>>
Left shift operator
<<

Now, let’s have a look at the truth table of bitwise operators. Because we need it to understand the operations of bitwise operator.

A B A & B A | B A ^ B
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0

Before starting, we recommend you to learn about conversion of number system from decimal to binary or from binary to decimal. If you already have the knowledge then let’s proceed.

Bitwise AND operator in C with demo program

Bitwise AND operator which is represented by & operator is one of the mostly used bitwise operators. It should be written between two variables. If both of the two bit is 1 then the and operator gives the output 1, otherwise it will give 0 at the result.

If we want the result of AND operator of the following x and y then the result will be the same as the result we have given bellow.

x = 0010 1101
y = 1001 1100

Result of x & y will be as following.

Result = 0000 1100

Let’s see the following program to learn more about bitwise AND operator.

#include <stdio.h>
int main(){
   int a = 7;          // binary equivalent = 0111
   int b = 12;         // binary equivalent = 1100
   int c = a & b;        // Result  will be = 0100

   printf("%d\n", c);     // equivalent of 0100 = 4
   return 0;
}

OR bitwise operators in C

Bitwise OR is also a mostly used bitwise operator which is represented by ‘ | ‘ sign. Two integers should be written on each side of this operator. The result of bitwise OR operator will be 0 if both of the inputs is 0. Otherwise, it will give 1 as the result.

The result of OR operator of the following x and y is given here.

x = 1010 1001
y = 1101 1101

Result of x | y will be as following.

Result = 1111 1101

See the following C program to understand bitwise OR operator.

#include <stdio.h>
int main(){
   int a = 17;       // binary equivalent of 17 = 10001
   int b = 22;       // binary equivalent of 22 = 10110
   int c = a | b;        // result in binary is = 10111

   printf("%d\n", c);     // equivalent decimal of 10111 is = 23
   return 0;
}

EX-OR bitwise operators with C programs

Bitwise Exclusive OR operator is represented by ‘ ^ ’ sign. Two integers are written on both side of this operator. EX-OR operator gives 1 in the output if any one of the input is 1. Otherwise, it will give 0 in the result.

Function of EX-OR operator in C programming is given bellow in the example.

x = 1110 1000
y = 1001 1100

Result of x ^ y will be as the following.

Result = 0111 0100

See the following C program to get a programming knowledge of bitwise EX-OR operator.

#include <stdio.h>
int main(){
   int x = 20;       // binary equivalent of 20 = 10100
   int y = 18;       // binary equivalent of 18 = 10010
   int z = x ^ y;        // result in binary is = 00110

   printf("%d\n", z);     // equivalent decimal of 00110 is = 6
   return 0;
}

One's complement bitwise operators with C

The bitwise one’s complement operator is also known as complement operator. It performs the operation as like not gate which can be our well known. It is expressed by ‘ ~ ’ symbol. It takes only one operand or variables and gives the output 1 if the input is 0 and gives the output 0 if the input is 1.

See the example bellow to get an idea of bitwise one’s complement operator in C.

a = 1010 1011

Result of ~a will be just opposite of a as given bellow.

Result = 0101 0100

Now, see the following code of C programming to understand programming knowledge of bitwise one’s complement operator.

#include <stdio.h>
int main(){
   int a = 22;     // binary equivalent of 22 = 10110
   int b = ~a;        // result in b will be  = 01001

   printf("%d\n", b);    // result in binary = -23
   return 0;
}

How we get -23 here?

Here integer a = 22.

Its equivalent binary number for 8 bit register is = 0 001 0110

One’s complement is = 1 110 1001

Here, first bit for 8 bit register is signed bit. And the result is in 2’s complement. So, lets try to get the equivalent value of the result in binary.

1 110 1001     ( 2’s complement )

1 110 1000    ( One’s complement by subtracting 1 )

1 001 0111    ( Actual number from taking the opposite bits )

Now, first bit is 1 which is signed bit and it refers that, this number will be negative. And value of remaining 7 bit is 23.

So, the output will be -23.

Right shift bitwise operators with C programs

Bitwise shift operators shift the number of bits to the left side or right side. Among two types of shift bitwise operators the right shift operator is used to shift the number of bits to the right side. So, the function of right shift operator is to shift the number of bits to right side.

Look at the following example of bitwise right shift operator.

x = 1110 1100

Result of shifting these bits at right by 2, a >> 2 is given here. 2 means we want to shift 2 bits here. You can try any number.

Result = 0011 1011

When we apply right shift operator, then the left bit will fill up by zero and mentioned bit will remove from the right side.

See for above example, as we want to shift 2 bit, then the two zeros have come to left and two bit have removed from the right side.

Let’s try to understand through a C program bellow.

#include <stdio.h>
int main(){
    int x = 18;     // binary equivalent of 18 = 10010
    int y = x>>3;        // value of y will be = 00010

    printf("%d\n", y);  // equivalent binary of 00010 is = 2
    return 0;
}

Left shift bitwise operators and their use

Bitwise left shift operator is used to shift the number of bits to the left side. For this, the function of left shift operator is to shift the number of bits to left side.

See the following example of bitwise left shift operator.

x = 1011 1110

Result of shifting to left by 2 digits a << 2 is given here. 2 means we want to shift 2 bits here. You can also try any number.

Result = 1111 1000

When we apply left shift bitwise operators, then the right bit will filled up by zero and mentioned bit will remove from left side.

In the above example, we want to shift 2 bit, then the two zeros have come to right side and two bit have removed from left side.

Let’s try to understand bitwise left shift operator through a C program bellow.

#include <stdio.h>
int main(){
    int x = 22;     // binary equivalent of 22 = 0001 0110
    int y = x << 2;      // value of y will be = 0101 1000

    printf("%d\n", y);  // equivalent binary of 01011000 is = 88
    return 0;
}
// Here everything is for 8 bit register