Let's take it bitwise!

Here's a detailed code sample illustrating the use of bitwise operators in C with explanations and output:

#include <stdio.h>

void displayBinary(int num) {
    // Display the binary representation of a number
    for (int i = sizeof(int) * 8 - 1; i >= 0; i--) {
        printf("%d", (num >> i) & 1);
        if (i % 4 == 0) printf(" ");  // Add space for better readability
    }
    printf("\n");
}

int main() {
    // Bitwise AND (&)
    int a = 14, b = 9;
    int andResult = a & b;
    printf("Bitwise AND: %d & %d = %d\n", a, b, andResult);
    displayBinary(a);
    displayBinary(b);
    displayBinary(andResult);

    // Bitwise OR (|)
    int orResult = a | b;
    printf("\nBitwise OR: %d | %d = %d\n", a, b, orResult);
    displayBinary(a);
    displayBinary(b);
    displayBinary(orResult);

    // Bitwise XOR (^)
    int xorResult = a ^ b;
    printf("\nBitwise XOR: %d ^ %d = %d\n", a, b, xorResult);
    displayBinary(a);
    displayBinary(b);
    displayBinary(xorResult);

    // Bitwise NOT (~)
    int notResult = ~a;
    printf("\nBitwise NOT: ~%d = %d\n", a, notResult);
    displayBinary(a);
    displayBinary(notResult);

    // Left Shift (<<)
    int leftShiftResult = a << 2;
    printf("\nLeft Shift: %d << 2 = %d\n", a, leftShiftResult);
    displayBinary(a);
    displayBinary(leftShiftResult);

    // Right Shift (>>)
    int rightShiftResult = a >> 2;
    printf("\nRight Shift: %d >> 2 = %d\n", a, rightShiftResult);
    displayBinary(a);
    displayBinary(rightShiftResult);

    return 0;
}

Explanation:

  1. Bitwise AND (&):

    • Performs a bitwise AND operation on each pair of corresponding bits.

    • Example: 14 & 9 = 8

    • Binary representation:

        14:  0000 0000 0000 0000 0000 0000 0000 1110
         9:  0000 0000 0000 0000 0000 0000 0000 1001
        & :  0000 0000 0000 0000 0000 0000 0000 1000
      
  2. Bitwise OR (|):

    • Performs a bitwise OR operation on each pair of corresponding bits.

    • Example: 14 | 9 = 15

    • Binary representation:

        14:  0000 0000 0000 0000 0000 0000 0000 1110
         9:  0000 0000 0000 0000 0000 0000 0000 1001
        | :  0000 0000 0000 0000 0000 0000 0000 1111
      
  3. Bitwise XOR (^):

    • Performs a bitwise XOR (exclusive OR) operation on each pair of corresponding bits.

    • Example: 14 ^ 9 = 7

    • Binary representation:

        14:  0000 0000 0000 0000 0000 0000 0000 1110
         9:  0000 0000 0000 0000 0000 0000 0000 1001
        ^ :  0000 0000 0000 0000 0000 0000 0000 0111
      
  4. Bitwise NOT (~):

    • Inverts each bit in the operand.

    • Example: ~14 = -15

    • Binary representation:

        14:  0000 0000 0000 0000 0000 0000 0000 1110
        ~ :  1111 1111 1111 1111 1111 1111 1111 0001
      
  5. Left Shift (<<):

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

    • Example: 14 << 2 = 56

    • Binary representation:

        14:  0000 0000 0000 0000 0000 0000 0000 1110
        <<:  0000 0000 0000 0000 0000 0000 0011 1000
      
  6. Right Shift (>>):

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

    • Example: 14 >> 2 = 3

    • Binary representation:

        14:  0000 0000 0000 0000 0000 0000 0000 1110
        >>:  0000 0000 0000 0000 0000 0000 0000 0011
      

Output:

Bitwise AND: 14 & 9 = 8
14: 0000 0000 0000 0000 0000 0000 0000 1110
 9: 0000 0000 0000 0000 0000 0000 0000 1001
& : 0000 0000 0000 0000 0000 0000 0000 1000

Bitwise OR: 14 | 9 = 15
14: 0000 0000 0000 0000 0000 0000 0000 1110
 9: 0000 0000 0000 0000 0000 0000 0000 1001
| : 0000 0000 0000 0000 0000 0000 0000 1111

Bitwise XOR: 14 ^ 9 = 7
14: 0000 0000 0000 0000 0000 0000 0000 1110
 9: 0000 0000 0000 0000 0000 0000 0000 1001
^ : 0000 0000 0000 0000 0000 0000 0000 0111

Bitwise NOT: ~14 = -15
14: 0000 0000 0000 0000 0000 0000 0000 1110
~ : 1111 1111 1111 1111 1111 1111 1111 0001

Left Shift: 14 << 2 = 56
14: 0000 0000 0000 0000 0000 0000 0000 1110
<<: 0000 0000 0000 0000 0000 0000 0011 1000

Right Shift: 14 >> 2 = 3
14: 0000 0000 0000 0000 0000 0000 0000 1110
>>: 0000 0000 0000 0000 0000 0000 0000 0011