C Bitwise Left Shift Operator

In C, the Bitwise Left Shift << operator is used to shift the bits of an operand to the left by a specified number of positions. Each left shift operation effectively multiplies the number by 2 raised to the power of the shift count.

Bitwise Left Shift Operator is commonly used in bit manipulation, low-level programming, and optimizing mathematical operations.


Syntax of the Bitwise Left Shift Operator

The syntax to use the Bitwise Left Shift operator is:

</>
Copy
result = value << shift_count;

Explanation:

  1. value: The integer whose bits will be shifted to the left.
  2. <<: The Bitwise Left Shift operator.
  3. shift_count: The number of positions to shift the bits.
  4. result: The resultant value after shifting value to the left.
  5. Each left shift multiplies the original number by 2^shift_count.

Examples of the Bitwise Left Shift Operator

1. Basic Left Shift Operation

In this example, we will apply the left shift operator on an integer to see how its value changes.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 5;
    int result = num << 1; // Left shift by 1

    printf("Original number: %d\n", num);
    printf("After left shift by 1: %d\n", result);

    return 0;
}

Explanation:

  1. We declare an integer num and assign it the value 5.
  2. The expression num << 1 shifts the bits of 5 to the left by 1 position.
  3. This effectively multiplies 5 by 2^1 = 2, resulting in 10.
  4. The result is stored in result and printed.

Output:

Original number: 5
After left shift by 1: 10

2. Left Shifting by Multiple Positions

In this example, we will shift an integer by multiple positions to observe the multiplication effect.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 3;
    int result = num << 3; // Left shift by 3

    printf("Original number: %d\n", num);
    printf("After left shift by 3: %d\n", result);

    return 0;
}

Explanation:

  1. We declare an integer num and assign it the value 3.
  2. The expression num << 3 shifts the bits of 3 to the left by 3 positions.
  3. This effectively multiplies 3 by 2^3 = 8, resulting in 24.
  4. The result is stored in result and printed.

Output:

Original number: 3
After left shift by 3: 24

3. Left Shifting Negative Numbers

In this example, we will apply the left shift operator to a negative integer and observe the result.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = -4;
    int result = num << 2; // Left shift by 2

    printf("Original number: %d\n", num);
    printf("After left shift by 2: %d\n", result);

    return 0;
}

Explanation:

  1. We declare an integer num with the value -4.
  2. The expression num << 2 shifts the bits of -4 to the left by 2 positions.
  3. Left shifting a negative number depends on the system’s representation (e.g., two’s complement), and results may vary.
  4. The result is stored in result and printed.

Output:

Original number: -4
After left shift by 2: -16

Conclusion

In this tutorial, we covered the Bitwise Left Shift << operator in C. The importance to remember are:

  1. The Bitwise Left Shift << operator shifts bits to the left.
  2. Each shift multiplies the number by 2^shift_count.
  3. It is commonly used for bit manipulation and efficient mathematical calculations.