In C language, bitwise operators are used to perform operations on individual bits of integers. These operators allow direct manipulation of bits, making them useful for tasks such as low-level programming, optimization, and systems programming. Bitwise operations are fast and efficient for various applications like setting, clearing, or flipping particular bits.

List of Bitwise Operators in C

  1. Bitwise AND (&):

    • Performs a logical AND operation on each bit of the operands.
    • Resulting bit is 1 only if both bits are 1.
    • Example:
      int a = 5; // 0101 in binary int b = 3; // 0011 in binary int result = a & b; // 0001 in binary, which is 1
  2. Bitwise OR (|):

    • Performs a logical OR operation on each bit of the operands.
    • Resulting bit is 1 if at least one bit is 1.
    • Example:
      int a = 5; // 0101 in binary int b = 3; // 0011 in binary int result = a | b; // 0111 in binary, which is 7
  3. Bitwise XOR (^):

    • Performs a logical XOR (exclusive OR) operation on each bit of the operands.
    • Resulting bit is 1 if one bit is 1 and the other is 0.
    • Example:
      int a = 5; // 0101 in binary int b = 3; // 0011 in binary int result = a ^ b; // 0110 in binary, which is 6
  4. Bitwise NOT (~):

    • Inverts all bits of the operand (changes 1 to 0 and 0 to 1).
    • This is a unary operator (applies to one operand).
    • Example:
      int a = 5; // 0101 in binary int result = ~a; // 1010 in binary (in two's complement representation), which is -6
  5. Left Shift (<<):

    • Shifts bits of the first operand to the left by the number of positions specified by the second operand.
    • Equivalent to multiplying by 2^n.
    • Example:
      int a = 5; // 0101 in binary int result = a << 1; // 1010 in binary, which is 10
  6. Right Shift (>>):

    • Shifts bits of the first operand to the right by the number of positions specified by the second operand.
    • Equivalent to dividing by 2^n (discarding the remainder for integers).
    • Example:
      int a = 20; // 10100 in binary int result = a >> 2; // 00101 in binary, which is 5

Bitwise Operator Examples

Here's a complete example of how to use bitwise operators in a C program:

#include <stdio.h> int main() { int a = 6; // 0110 in binary int b = 3; // 0011 in binary printf("a & b = %d\n", a & b); // 0010 in binary, which is 2 printf("a | b = %d\n", a | b); // 0111 in binary, which is 7 printf("a ^ b = %d\n", a ^ b); // 0101 in binary, which is 5 printf("~a = %d\n", ~a); // 1001 in binary (in two's complement), which is -7 printf("a << 1 = %d\n", a << 1); // 1100 in binary, which is 12 printf("b >> 1 = %d\n", b >> 1); // 0001 in binary, which is 1 return 0; }

Summary of Bitwise Operators:

  1. & (AND): Returns 1 if both bits are 1.
  2. | (OR): Returns 1 if at least one bit is 1.
  3. ^ (XOR): Returns 1 if one bit is 1 and the other is 0.
  4. ~ (NOT): Inverts all bits.
  5. << (Left Shift): Shifts bits to the left, effectively multiplying by 2^n.
  6. >> (Right Shift): Shifts bits to the right, effectively dividing by 2^n.

Important Points:

  1. Efficiency: Bitwise operators are highly efficient, particularly in low-level programming, such as working with hardware registers or network protocols.
  2. Two's Complement: The ~ operator follows the two's complement representation for negative numbers, which may lead to unexpected results for beginners.
  3. Unsigned vs. Signed: For left and right shifts, be careful with signed integers. The right shift for signed values may be implementation-dependent, depending on whether it's arithmetic or logical shift.

Bitwise operators are powerful tools that allow fine control over individual bits, making them essential for certain types of optimizations and low-level programming tasks.