C Programming
About Lesson
  • Lesson 1: Bitwise Operators

    Bitwise operators in C are used to perform operations at the bit-level of integers. These operators manipulate individual bits within integer operands.

    • Bitwise AND (&):

      • Sets each bit to 1 if both corresponding bits of the operands are 1.
       
      result = a & b;
      • Sets each bit to 1 if at least one of the corresponding bits of the operands is 1.

        Bitwise OR (|):

    • result = a | b;
    • Bitwise XOR (^):

      • Sets each bit to 1 if only one of the corresponding bits of the operands is 1.
      result = a ^ b;
    • Bitwise NOT (~):

      • Flips all bits of the operand (1 becomes 0 and 0 becomes 1).
       
      result = ~a;
    Lesson 2: Bitwise Shift Operators

    Bitwise shift operators (<< for left shift, >> for right shift) move the bits of a number to the left or right by a specified number of positions.

    • Left Shift (<<):

      • Shifts the bits of a number to the left by a specified number of positions.
       
      result = a << n; // Left shift a by n positions
    • Right Shift (>>):

      • Shifts the bits of a number to the right by a specified number of positions.
       
      result = a >> n; // Right shift a by n positions
    Lesson 3: Bit Manipulation Techniques

    Bit manipulation involves using bitwise operators to perform specific tasks such as setting, clearing, toggling, or checking individual bits within an integer.

    • Setting a Bit:

      • Use bitwise OR (|) with a mask to set a specific bit to 1.
       
      num |= (1 << bitPos); // Set bit at position bitPos
    • Clearing a Bit:

      • Use bitwise AND (&) with the complement of a mask to clear a specific bit to 0.
      num &= ~(1 << bitPos); // Clear bit at position bitPos
    • Toggling a Bit:

      • Use bitwise XOR (^) with a mask to toggle a specific bit (flip its value).
       
      num ^= (1 << bitPos); // Toggle bit at position bitPos
    • Checking a Bit:

      • Use bitwise AND (&) with a mask to check the value of a specific bit.
       
      if (num & (1 << bitPos)) {
      // Bit at position bitPos is set (1)
      } else {
      // Bit at position bitPos is clear (0)
      }
    Lesson 4: Practice and Examples
    • Practice Exercise:
      • Write a function to count the number of set bits (1s) in an integer using bitwise operations.
      • Implement a function to swap two integers without using a temporary variable (using bitwise XOR).
      • Create a program that checks if a number is a power of 2 using bitwise operations.
     
    #include <stdio.h>

    // Function to count the number of set bits (1s) in an integer
    int countSetBits(int num) {
    int count = 0;
    while (num != 0) {
    count += num & 1;
    num >>= 1;
    }
    return count;
    }

    // Function to swap two integers using bitwise XOR
    void swapIntegers(int *a, int *b) {
    *a = *a ^ *b;
    *b = *a ^ *b;
    *a = *a ^ *b;
    }

    // Function to check if a number is a power of 2
    int isPowerOfTwo(int num) {
    return (num > 0) && ((num & (num - 1)) == 0);
    }

    int main() {
    int num1 = 10, num2 = 20;

    printf("Number of set bits in %d: %dn", num1, countSetBits(num1));

    printf("Before swapping: num1 = %d, num2 = %dn", num1, num2);
    swapIntegers(&num1, &num2);
    printf("After swapping: num1 = %d, num2 = %dn", num1, num2);

    int testNum = 16;
    if (isPowerOfTwo(testNum)) {
    printf("%d is a power of 2n", testNum);
    } else {
    printf("%d is not a power of 2n", testNum);
    }

    return 0;
    }

    Bitwise operators and bit manipulation techniques are powerful tools for low-level programming tasks in C. Practice using bitwise AND, OR, XOR, shift operators (<<, >>), and bit manipulation techniques (&, |, ^, ~) to perform efficient bit-level operations on integers. Experiment with implementing common algorithms and tasks using bitwise operations to deepen your understanding of bitwise programming in C. Understanding bit manipulation is essential for tasks like low-level hardware interaction, optimizing memory usage, and implementing efficient algorithms in embedded systems and system programming.