C Left Shift Assignment Operator

In C, the Left Shift Assignment <<= operator is used to shift the bits of a variable to the left by a specified number of positions and assign the result back to the variable.

Left Shift Assignment operation effectively multiplies the variable by 2^n, where n is the number of positions to shift. It is commonly used for optimizing multiplication by powers of two.


Syntax of the Left Shift Assignment Operator

The syntax to use the Left Shift Assignment operator is:

</>
Copy
variable <<= shift_count;

Explanation:

  1. variable: The variable whose bits are to be shifted.
  2. <<=: The left shift assignment operator.
  3. shift_count: The number of positions to shift the bits to the left.
  4. Return Value: The left-shifted value is assigned back to variable.

Examples of the Left Shift Assignment Operator

1. Basic Left Shift Assignment

In this example, we will use the <<= operator to shift the bits of a number two positions to the left.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 5;
    num <<= 2; // Left shift by 2 positions

    printf("Result after left shift: %d\n", num);
    return 0;
}

Explanation:

  1. We initialize an integer variable num with a value of 5.
  2. The statement num <<= 2 shifts the bits of num two positions to the left.
  3. Since a left shift by one position multiplies the number by 2, shifting by two positions multiplies it by 2^2 = 4.
  4. The new value of num is 5 × 4 = 20, which is printed as output.

Output:

Result after left shift: 20

2. Left Shift Assignment in a Loop

In this example, we will apply the <<= operator inside a for loop to demonstrate progressive left shifts.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 1;

    // Left shift the value progressively
    for (int i = 0; i < 4; i++) {
        num <<= 1;
        printf("After shift %d: %d\n", i + 1, num);
    }

    return 0;
}

Explanation:

  1. We initialize an integer num with a value of 1.
  2. A for loop runs four times, shifting num by one position in each iteration.
  3. Each left shift doubles the previous value of num.
  4. The output shows how the value changes after each shift.

Output:

After shift 1: 2
After shift 2: 4
After shift 3: 8
After shift 4: 16

3. Left Shift Assignment with Negative Numbers

In this example, we will observe the behavior of the <<= operator when used with negative numbers.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = -3;
    num <<= 1; // Left shift by 1 position

    printf("Result after left shift: %d\n", num);
    return 0;
}

Explanation:

  1. We initialize an integer num with a value of -3.
  2. The statement num <<= 1 shifts the bits of num one position to the left.
  3. Since negative numbers are stored in two’s complement form, the behavior may be compiler-dependent.
  4. Some systems might preserve the sign, while others may lead to unexpected results.

Output:

Result after left shift: -6

Conclusion

In this tutorial, we covered the Left Shift Assignment <<= operator in C:

  1. The operator shifts bits to the left and assigns the new value to the variable.
  2. Each left shift by n positions multiplies the value by 2^n.
  3. It is commonly used for efficient multiplication by powers of two.
  4. When used with negative numbers, behavior can vary based on the compiler.