Bit-wise Operators in C

By | February 18, 2023

Prerequisite – Operators in C
In C, bitwise operators are used to perform operations on the binary representation of numbers. These operators can be used to manipulate individual bits in a variable, which can be useful in certain situations such as when working with hardware or networking protocols.

Types of Bit-wise Operators in C:
There are six bitwise operators in C:

  1. Bitwise AND (&)
  2. Bitwise OR (|)
  3. Bitwise XOR (^)
  4. Bitwise NOT (~)
  5. Left shift (<<)
  6. Right shift (>>)

Here’s a description of each operator and how they work:

  1. Bitwise AND (&): This operator performs a bitwise AND operation between the binary representations of two integers. The result is an integer with a 1 bit in each position where both operands have a 1 bit. For example, if a is 0b1101 (13 in decimal) and b is 0b1011 (11 in decimal), a & b would be 0b1001 (9 in decimal).
  2. Bitwise OR (|): This operator performs a bitwise OR operation between the binary representations of two integers. The result is an integer with a 1 bit in each position where either operand has a 1 bit. For example, if a is 0b1101 (13 in decimal) and b is 0b1011 (11 in decimal), a | b would be 0b1111 (15 in decimal).
  3. Bitwise XOR (^): This operator performs a bitwise exclusive OR operation between the binary representations of two integers. The result is an integer with a 1 bit in each position where the operands have different bits. For example, if a is 0b1101 (13 in decimal) and b is 0b1011 (11 in decimal), a ^ b would be 0b0110 (6 in decimal).
  4. Bitwise NOT (~): This operator performs a bitwise NOT operation on the binary representation of an integer. The result is an integer with all bits inverted (i.e. 0 bits become 1 bits and vice versa). For example, if a is 0b1101 (13 in decimal), ~a would be 0b0010 (-14 in decimal due to two’s complement representation).
  5. Left shift (<<): This operator shifts the binary representation of an integer to the left by a specified number of bits. The result is an integer with the specified number of 0 bits added to the right. For example, if a is 0b1101 (13 in decimal), a << 2 would be 0b110100 (52 in decimal).
  6. Right shift (>>): This operator shifts the binary representation of an integer to the right by a specified number of bits. The result is an integer with the specified number of bits removed from the right. For example, if a is 0b1101 (13 in decimal), a >> 2 would be 0b0011 (3 in decimal).

Example:
Here’s an example code that demonstrates the use of bitwise operators in C:

#include <stdio.h>

int main() {
    int a = 13; // binary: 1101
    int b = 11; // binary: 1011
    int c;

    // Bitwise AND operator
    c = a & b; // binary: 1001 (9 in decimal)
    printf("a & b = %d\n", c);

    // Bitwise OR operator
    c = a | b; // binary: 1111 (15 in decimal)
    printf("a | b = %d\n", c);

    // Bitwise XOR operator
    c = a ^ b; // binary: 0110 (6 in decimal)
    printf("a ^ b = %d\n", c);

    // Bitwise complement operator
    c = ~a; // binary: 0010 (in 2's complement representation)
    printf("~a = %d\n", c);

    // Left shift operator
    c = a << 1; // binary: 11010 (26 in decimal)
    printf("a << 1 = %d\n", c);

    // Right shift operator
    c = b >> 1; // binary: 0101 (5 in decimal)
    printf("b >> 1 = %d\n", c);

    return 0;
}

In this example, we declare two integer variables a and b, with the values 13 and 11, respectively. We then perform various bitwise operations on these variables and store the results in a third variable c. We print out the value of c after each operation.

The output of this program will be:

a & b = 9
a | b = 15
a ^ b = 6
~a = -14
a << 1 = 26
b >> 1 = 5

As you can see, the bitwise operations on these values are performed according to the same rules as for the previous example. The values of c are different, of course, because we are using different values for a and b.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.