C Programs Tutorials | IT Developer
IT Developer

C Programming - C Bitwise Operators



Share with a Friend

C Programming - C Bitwise Operators

C Bitwise Operators

Bitwise operators in C perform operations directly on the binary representations of integers. These operators manipulate individual bits of data, making them highly efficient for low-level programming, such as embedded systems, hardware interfacing, and performance-critical applications.

List of Bitwise Operators

Operator Name Description Example Result

&

Bitwise AND

Performs AND operation on each bit.

a & b

0100 (binary)

`

`

Bitwise OR

Performs OR operation on each bit.

`a

^

Bitwise XOR

Performs XOR (exclusive OR) operation on each bit.

a ^ b

1010 (binary)

~

Bitwise NOT (One's Complement)

Flips all bits (1 to 0, and 0 to 1).

~a

1011 (binary)

<< 

Left Shift

Shifts bits to the left by a specified number of positions.

a << 2

1100 (binary)

>> 

Right Shift

Shifts bits to the right by a specified number of positions.

a >> 2

0011 (binary)

Key Characteristics

  1. Bitwise AND (&):
    • Compares each bit of the operands and sets the corresponding bit in the result to 1 only if both bits are 1.

C

int a = 12, b = 10; // a = 1100, b = 1010

int result = a & b; // result = 1000 (8 in decimal)

  1. Bitwise OR (|):
    • Compares each bit of the operands and sets the corresponding bit in the result to 1 if at least one bit is 1.

C

int a = 12, b = 10; // a = 1100, b = 1010

int result = a | b; // result = 1110 (14 in decimal)

  1. Bitwise XOR (^):
    • Compares each bit of the operands and sets the corresponding bit in the result to 1 if the bits are different.

C

int a = 12, b = 10; // a = 1100, b = 1010

int result = a ^ b; // result = 0110 (6 in decimal)

  1. Bitwise NOT (~):
    • Inverts all bits (flips 1 to 0 and 0 to 1).

C

int a = 12;         // a = 1100

int result = ~a;    // result = 0011 (in 2's complement: -13 in decimal)

  1. Left Shift (<<):
    • Shifts the bits of the operand to the left by the specified number of positions. The vacant bits on the right are filled with 0.

C

int a = 3;          // a = 0011

int result = a << 2; // result = 1100 (12 in decimal)

  1. Right Shift (>>):
    • Shifts the bits of the operand to the right by the specified number of positions. The vacant bits on the left depend on the type of shift (logical or arithmetic).

C

int a = 12;         // a = 1100

int result = a >> 2; // result = 0011 (3 in decimal)

Example Program

C

#include <stdio.h>

int main() {

    int a = 12, b = 10;

    printf("a & b = %d\n", a & b);  // Bitwise AND

    printf("a | b = %d\n", a | b);  // Bitwise OR

    printf("a ^ b = %d\n", a ^ b);  // Bitwise XOR

    printf("~a = %d\n", ~a);        // Bitwise NOT

    printf("a << 2 = %d\n", a << 2); // Left Shift

    printf("a >> 2 = %d\n", a >> 2); // Right Shift

    return 0;

}

Output:

a & b = 8

a | b = 14

a ^ b = 6

~a = -13

a << 2 = 48

a >> 2 = 3

Applications of Bitwise Operators

  1. Masking Bits:
    • Used to extract specific bits from a binary number.

C

int num = 0b10101010;

int mask = 0b00001111;

int result = num & mask; // Extracts the lower 4 bits

  1. Setting/Clearing Specific Bits:
    • Set a bit: num | (1 << n)
    • Clear a bit: num & ~(1 << n)

C

int num = 0b10101010;

num |= (1 << 3);  // Set the 4th bit

num &= ~(1 << 2); // Clear the 3rd bit

  1. Toggling Bits:
    • Flip specific bits using XOR.

C

int num = 0b10101010;

num ^= (1 << 3); // Toggle the 4th bit

  1. Checking Specific Bits:
    • Check if a bit is set: (num & (1 << n)) != 0

C

int num = 0b10101010;

if (num & (1 << 3)) {

    printf("4th bit is set\n");

}

  1. Efficient Multiplication/Division:
    • Multiply by 2^n: num << n
    • Divide by 2^n: num >> n

C

int num = 5;

int result = num << 3; // Multiplies num by 8

Points to Remember

  1. Bitwise operations are performed on integers only.
  2. Be cautious with signed integers during shifts, as results may vary.
  3. Use parentheses for clarity in complex expressions involving bitwise operators.
  4. Use bitwise operators in scenarios requiring performance-critical optimizations.