C - Bitwise Operator

Bitwise operators in C are used to perform operations on individual bits of integer values. These operators allow you to manipulate binary representations of data at the bit level. There are several bitwise operators in C:

1. Bitwise AND (&) Operator:

The Bitwise AND (&) operator is used in C to perform a bitwise AND operation between corresponding bits of two integer operands. It is one of the bitwise operators that work at the binary level, manipulating individual bits of binary representations of numbers.

Here's a more detailed explanation of the Bitwise AND (&) operator:

  • 'Syntax': operand1 & operand2
  • 'Description': The Bitwise AND operator compares each pair of corresponding bits in operand1 and operand2. If both bits in a pair are 1, the result bit is 1. Otherwise, the result bit is 0.

Key points about the Bitwise AND (&) operator:

  • It operates on the binary representation of integers, considering each bit position independently.
  • The result of a Bitwise AND operation is 1 only if both corresponding bits in the operands are 1. If at least one bit is 0, the result bit will be 0.
  • It is often used for tasks like bit masking and clearing specific bits in integers.

Let's illustrate the Bitwise AND (&) operator step by step with an example.

Suppose we have two integer variables a and b, and we want to perform a bitwise AND operation between their binary representations.


#include <stdio.h>

int main() {
    int a = 5; // Binary: 0101
    int b = 3; // Binary: 0011
    int result;

    // Bitwise AND (&) operator
    result = a & b;

    printf("a & b = %d\n", result);

    return 0;
}

Now, let's break down how the Bitwise AND (&) operator works in this example:

  1. We declare two integer variables, a and b, with values 5 and 3, respectively. The binary representations of these values are 0101 and 0011.
  2. We use the Bitwise AND (&) operator to perform a bitwise AND operation between a and b.
  3. The binary representation of a is 0101, and the binary representation of b is 0011. When we perform a bitwise AND operation between them, we compare each pair of corresponding bits:
    • The first pair (0 & 0) results in 0.
    • The second pair (1 & 0) results in 0.
    • The third pair (0 & 1) results in 0.
    • The fourth pair (1 & 1) results in 1.
  4. The result of the bitwise AND operation is 0001, which is 1 in decimal notation.
  5. We assign the result to the result variable.
  6. Finally, we print the result using printf, which will display "a & b = 1" on the screen.

So, the output of this program is:


a & b = 1

This demonstrates how the Bitwise AND (&) operator works by performing a bitwise AND operation between the corresponding bits of two integers. It sets each bit in the result to 1 if both corresponding bits in the operands are 1.

2. Bitwise OR (|) Operator:

The Bitwise OR (|) operator is used in C to perform a bitwise OR operation between corresponding bits of two integer operands. It is one of the bitwise operators that work at the binary level, manipulating individual bits of binary representations of numbers.

Here's a more detailed explanation of the Bitwise OR (|) operator:

  • 'Syntax': operand1 | operand2
  • 'Description': The Bitwise OR operator compares each pair of corresponding bits in operand1 and operand2. If at least one bit in a pair is 1, the result bit is 1. Only if both bits are 0, the result bit is 0.

Key points about the Bitwise OR (|) operator:

  • It operates on the binary representation of integers, considering each bit position independently.
  • The result of a Bitwise OR operation is 1 if at least one of the corresponding bits in the operands is 1. If both bits are 0, the result bit will be 0.
  • It is often used for tasks like setting specific bits in integers or combining flag values.

Let's illustrate the Bitwise OR (|) operator step by step with an example.

Suppose we have two integer variables a and b, and we want to perform a bitwise OR operation between their binary representations.


#include <stdio.h>

int main() {
    int a = 5; // Binary: 0101
    int b = 3; // Binary: 0011
    int result;

    // Bitwise OR (|) operator
    result = a | b;

    printf("a | b = %d\n", result);

    return 0;
}

Now, let's break down how the Bitwise OR (|) operator works in this example:

  1. We declare two integer variables, a and b, with values 5 and 3, respectively. The binary representations of these values are 0101 and 0011.
  2. We use the Bitwise OR (|) operator to perform a bitwise OR operation between a and b.
  3. The binary representation of a is 0101, and the binary representation of b is 0011. When we perform a bitwise OR operation between them, we compare each pair of corresponding bits:
    • The first pair (0 | 0) results in 0.
    • The second pair (1 | 0) results in 1.
    • The third pair (0 | 1) results in 1.
    • The fourth pair (1 | 1) results in 1.
  4. The result of the bitwise OR operation is 0111, which is 7 in decimal notation.
  5. We assign the result to the result variable.
  6. Finally, we print the result using printf, which will display "a | b = 7" on the screen.
  7. So, the output of this program is:

    
    a | b = 7
    

    This demonstrates how the Bitwise OR (|) operator works by performing a bitwise OR operation between the corresponding bits of two integers. It sets each bit in the result to 1 if at least one of the corresponding bits in the operands is 1.

    3. Bitwise XOR (^) Operator:

    The Bitwise XOR (^) operator is used in C to perform a bitwise XOR (exclusive OR) operation between corresponding bits of two integer operands. It is one of the bitwise operators that work at the binary level, manipulating individual bits of binary representations of numbers.

    Here's a more detailed explanation of the Bitwise XOR (^) operator:

    • 'Syntax': operand1 ^ operand2
    • 'Description': The Bitwise XOR operator compares each pair of corresponding bits in operand1 and operand2. If the bits in a pair are different (one is 0 and the other is 1), the result bit is set to 1. If the bits in a pair are the same (both 0 or both 1), the result bit is set to 0.

    Key points about the Bitwise XOR (^) operator:

    • It operates on the binary representation of integers, considering each bit position independently.
    • The result of a Bitwise XOR operation is 1 if the corresponding bits in the operands are different (one 0 and the other 1). If both bits are the same (both 0 or both 1), the result bit will be 0.
    • It is often used for tasks like flipping specific bits in integers, toggling flags, and performing certain cryptographic operations.

    Suppose we have two integer variables a and b, and we want to perform a bitwise XOR operation between their binary representations.

    
    #include <stdio.h>
    
    int main() {
        int a = 5; // Binary: 0101
        int b = 3; // Binary: 0011
        int result;
    
        // Bitwise XOR (^) operator
        result = a ^ b;
    
        printf("a ^ b = %d\n", result);
    
        return 0;
    }
    

    Now, let's break down how the Bitwise XOR (^) operator works in this example:

    1. We declare two integer variables, a and b, with values 5 and 3, respectively. The binary representations of these values are 0101 and 0011.
    2. We use the Bitwise XOR (^) operator to perform a bitwise XOR operation between a and b.
    3. The binary representation of a is 0101, and the binary representation of b is 0011. When we perform a bitwise XOR operation between them, we compare each pair of corresponding bits:
      • The first pair (0 ^ 0) results in 0.
      • The second pair (1 ^ 0) results in 1.
      • The third pair (0 ^ 1) results in 1.
      • The fourth pair (1 ^ 1) results in 0.
      The result of the bitwise XOR operation is 0110, which is 6 in decimal notation.
    4. We assign the result to the result variable.
    5. Finally, we print the result using printf, which will display "a ^ b = 6" on the screen.

    So, the output of this program is:

    
    a ^ b = 6
    

    This demonstrates how the Bitwise XOR (^) operator works by performing a bitwise XOR operation between the corresponding bits of two integers. It sets each bit in the result to 1 if the corresponding bits in the operands are different (one 0 and the other 1).

    4. Bitwise NOT (~) Operator:

    The Bitwise NOT (~) operator is used in C to perform a bitwise NOT operation on a single integer operand. It is a unary operator, meaning it operates on a single operand. The Bitwise NOT operator inverts each bit of the operand, changing 0 to 1 and 1 to 0.

    Here's a more detailed explanation of the Bitwise NOT (~) operator:

    • 'Syntax': ~operand
    • 'Description': The Bitwise NOT operator inverts each bit of operand. Each 0 bit becomes 1, and each 1 bit becomes 0.

    Key points about the Bitwise NOT (~) operator:

    1. It operates on the binary representation of the integer operand, flipping each bit.
    2. The result of a Bitwise NOT operation is the one's complement of the operand. It inverts all the bits in the binary representation.
    3. It is often used for tasks like toggling or inverting specific bits within an integer or creating bit masks.

    Let's illustrate the Bitwise NOT (~) operator step by step with an example.

    Suppose we have an integer variable a, and we want to perform a bitwise NOT operation on its binary representation.

    
    #include <stdio.h>
    
    int main() {
        int a = 5; // Binary: 0101
        int result;
    
        // Bitwise NOT (~) operator
        result = ~a;
    
        printf("~a = %d\n", result);
    
        return 0;
    }
    

    Now, let's break down how the Bitwise NOT (~) operator works in this example:

    1. We declare an integer variable a with the value 5. The binary representation of ~ is 0101.
    2. We use the Bitwise NOT (~) operator to perform a bitwise NOT operation on a.
    3. The binary representation of a is 0101. When we perform a bitwise NOT operation, each bit is inverted:
      • The first bit 0 becomes 1.
      • The second bit 1 becomes 0.
      • The third bit 0 becomes 1.
      • The fourth bit 1 becomes 0.
    4. The result of the bitwise NOT operation is 0101, which is the one's complement of the binary representation of 5.
    5. We assign the result to the result variable.
    6. Finally, we print the result using printf, which will display "~a = -6" on the screen.

    So, the output of this program is:

    
    ~a = -6
    

    This demonstrates how the Bitwise NOT (~) operator works by inverting each bit in the binary representation of the operand. It changes 0 to 1 and 1 to 0, effectively creating the one's complement of the original value.

    5. Left Shift (<<) Operator:

    The Left Shift (<<) operator is used in C to perform a bitwise left shift operation on the binary representation of an integer. It shifts the bits of the integer to the left by a specified number of positions. The vacant positions on the right are filled with zeros. This operation effectively multiplies the integer by 2 raised to the power of the shift count.

    Here's a more detailed explanation of the Left Shift (<<) operator:

    • 'Syntax': operand << shift_count
    • 'Description': The Left Shift operator shifts the bits of operand to the left by shift_count positions. The leftmost bits are shifted out, and zeros are added on the right.

    Key points about the Left Shift (<<) operator:

    1. It operates on the binary representation of the integer operand, shifting each bit leftward.
    2. The result of a Left Shift operation is the integer value obtained by shifting the bits. It is equivalent to multiplying the operand by 2 raised to the power of shift_count.
    3. It is commonly used for bit manipulation, creating bit patterns, and optimizing multiplication or division by powers of 2.

    Suppose we have an integer variable a, and we want to perform a bitwise left shift operation on its binary representation by a specified number of positions.

    
    #include <stdio.h>
    
    int main() {
        int a = 5; // Binary: 0000 0101
        int result;
    
        // Left Shift (<<) operator
        result = a << 2; // Left shift by 2 positions
    
        printf("a << 2 = %d\n", result);
    
        return 0;
    }
    

    Now, let's break down how the Left Shift (<<) operator works in this corrected example:

    1. We declare an integer variable a with the value 5. The binary representation of 5 is 0000 0101.
    2. We use the Left Shift (<<) operator to perform a bitwise left shift operation on a by 2 positions.
    3. The binary representation of a is 0000 0101. When we perform a left shift by 2 positions, the bits are shifted to the left, and two zeros are added on the right:
      • The two leftmost bits are shifted out.
      • Two zeros (00) are added to the right.
    4. The result of the left shift operation is 0001 0100, which is 20 in decimal notation.
    5. We assign the result to the result variable.
    6. Finally, we print the result using printf, which will display "a << 2 = 20" on the screen.

    So, the output of this corrected program is:

    
    a << 2 = 20
    

    This demonstrates how the Left Shift (<<) operator works by shifting the bits of an integer to the left by a specified number of positions, effectively multiplying the integer by 2^shift_count. In this case, it multiplies 5 by 4, resulting in 20.

    6. Right Shift (>>) Operator:

    The Right Shift (>>) operator is used in C to perform a bitwise right shift operation on the binary representation of an integer. It shifts the bits of the integer to the right by a specified number of positions. Depending on the type of integer being shifted, the vacant positions on the left are either filled with zeros or the sign bit (for signed integers).

    Here's a more detailed explanation of the Right Shift (>>) operator:

    • 'Syntax': operand >> shift_count
    • 'Description': The Right Shift operator shifts the bits of operand to the right by shift_count positions. Depending on the type of operand (signed or unsigned), the vacant positions on the left are filled with zeros or the sign bit.

    Key points about the Right Shift (>>) operator:

    1. It operates on the binary representation of the integer operand, shifting each bit rightward.
    2. For signed integers, the result of a Right Shift operation can be influenced by the sign bit. In most implementations, the vacant positions are filled with the sign bit (arithmetic right shift).
    3. For unsigned integers, the result is filled with zeros in the vacant positions (logical right shift).
    4. It is commonly used for dividing an integer by powers of 2, extracting specific fields of bits, and optimizing code for certain operations.

    Let's illustrate the Right Shift (>>) operator step by step with an example, specifically for a signed integer.

    Suppose we have a signed integer variable a, and we want to perform a bitwise right shift operation on its binary representation by a specified number of positions.

    
    #include <stdio.h>
    
    int main() {
        int a = -16; // Binary: 1111 1111 1111 1111 1111 1111 1111 0000
        int result;
    
        // Right Shift (>>) operator
        result = a >> 2; // Right shift by 2 positions
    
        printf("a >> 2 = %d\n", result);
    
        return 0;
    }
    

    Now, let's break down how the Right Shift (>>) operator works in this example for a signed integer:

    1. We declare a signed integer variable a with the value -16. The binary representation of -16 depends on the two's complement representation and is 1111 1111 1111 1111 1111 1111 1111 0000.
    2. We use the Right Shift (>>) operator to perform a bitwise right shift operation on a by 2 positions.
    3. The binary representation of a is 1111 1111 1111 1111 1111 1111 1111 0000. When we perform a right shift by 2 positions, the bits are shifted to the right:
      • The two rightmost bits are shifted out.
      • Depending on the implementation (two's complement), the vacant positions on the left are filled with the sign bit, which is 1 in this case (arithmetic right shift).
    4. The result of the right shift operation is 1111 1111 1111 1111 1111 1111 1111 1100, which represents -4 in decimal notation.
    5. We assign the result to the result variable.
    6. Finally, we print the result using printf, which will display "a >> 2 = -4" on the screen.

    So, the output of this program is:

    
    a >> 2 = -4
    

    This demonstrates how the Right Shift (>>) operator works for a signed integer by shifting the bits of the integer to the right by a specified number of positions, taking into account the sign bit. In this case, it effectively divides -16 by 2^2, resulting in -4.