Lesson 12-C Language Bit Operations

Bit Operators

C language has the following bit operators:

  • & (AND): Bitwise AND
  • | (OR): Bitwise OR
  • ^ (XOR): Bitwise XOR
  • ~ (NOT): Bitwise NOT
  • << (Left Shift): Left shift
  • >> (Right Shift): Right shift

Applications of Bit Operations

  • Bitwise AND (&): The result is 1 only when both corresponding binary bits are 1.
  • Bitwise OR (|): The result is 1 if at least one of the corresponding binary bits is 1.
  • Bitwise XOR (^): The result is 1 when the corresponding binary bits are different.
  • Bitwise NOT (~): Changes 1 to 0 and 0 to 1 in binary bits.
  • Left Shift (<<): Shifts binary bits to the left by the specified number of positions, filling vacated positions on the right with 0.
  • Right Shift (>>): Shifts binary bits to the right by the specified number of positions, filling vacated positions on the left with the sign bit (for signed integers) or 0 (for unsigned integers).

Bit Operation Examples

Below are some specific examples of bit operations.

Example 1: Bitwise AND

#include <stdio.h>

int main() {
    int a = 12; // Binary: 1100
    int b = 7;  // Binary: 0111
    int c = a & b; // Result: 0100 (4)

    printf("a & b = %d\n", c);
    return 0;
}

Example 2: Bitwise OR

#include <stdio.h>

int main() {
    int a = 12; // Binary: 1100
    int b = 7;  // Binary: 0111
    int c = a | b; // Result: 1111 (15)

    printf("a | b = %d\n", c);
    return 0;
}

Example 3: Bitwise XOR

#include <stdio.h>

int main() {
    int a = 12; // Binary: 1100
    int b = 7;  // Binary: 0111
    int c = a ^ b; // Result: 1011 (11)

    printf("a ^ b = %d\n", c);
    return 0;
}

Example 4: Bitwise NOT

#include <stdio.h>

int main() {
    int a = 12; // Binary: 1100
    int c = ~a; // Result depends on integer size and compiler implementation

    printf("~a = %d\n", c);
    return 0;
}

Example 5: Left Shift

#include <stdio.h>

int main() {
    int a = 12; // Binary: 1100
    int c = a << 2; // Result: 110000 (48)

    printf("a << 2 = %d\n", c);
    return 0;
}

Example 6: Right Shift

#include <stdio.h>

int main() {
    int a = 12; // Binary: 1100
    int c = a >> 2; // Result: 0011 (3)

    printf("a >> 2 = %d\n", c);
    return 0;
}

Practical Applications of Bit Operations

Bit operations have many uses in real programming, such as setting flags, bit masks, bit fields, etc.

Example 7: Setting a Bit

#include <stdio.h>

int main() {
    unsigned char flag = 0; // Initial value 0
    unsigned char mask = 1 << 3; // Set the 4th bit to 1
    flag |= mask; // Set the 4th bit to 1

    printf("Flag after setting bit: %u\n", flag);
    return 0;
}

Example 8: Clearing a Bit

#include <stdio.h>

int main() {
    unsigned char flag = 12; // Binary: 1100
    unsigned char mask = ~(1 << 2); // Clear the 3rd bit
    flag &= mask; // Clear the 3rd bit

    printf("Flag after clearing bit: %u\n", flag);
    return 0;
}

Example 9: Toggling a Bit

#include <stdio.h>

int main() {
    unsigned char flag = 12; // Binary: 1100
    unsigned char mask = 1 << 1; // Toggle the 2nd bit
    flag ^= mask; // Toggle the 2nd bit

    printf("Flag after toggling bit: %u\n", flag);
    return 0;
}

Precautions for Bit Operations

  • When using bit operations, pay attention to the size of integers and the influence of the sign bit.
  • For signed integers, right shift operations may cause sign extension.
  • For unsigned integers, right shift operations always fill vacated positions on the left with 0.
Share your love