Using the Left Shift and Right Shift operators in C / C++

Filed Under: C Programming
Leftshift Rightshift C Cpp(1)

In this article, we’ll take a look at using the left shift and right shift operators in C/C++.

These two operators are very useful if you want to do some bit-manipulation operations.

Let’s look at them in more detail, using some examples.


Using the Left Shift Operator (<<)

The Left Shift (<<) operator is called as the arithmetic left shift operator.

This works for both signed as well as unsigned numbers.

Due to this, we can apply this to types like int, long, char, etc.

This operator left shifts the bits of the number accordingly, by the amount we specify.

shifted_value = old_value << amount;

For example, consider the integer 100. In a bit-wise format, we can write it as 1100100.

However, we did not write all the bits. If we assume that an integer is 4 bytes (32 bits), the complete byte representation (32 bits) will be:

00000000000000000000000001100100

If we want to left-shift this number by 2 units, what happens?

The number becomes 400, that is:

00000000000000000000000110010000

This means that we multiply the integer by (2^amount)!

If amount = 2, we will multiply 100 with 2^2 = 4, so our final result is 400!

#include <stdio.h>

int main() {
    unsigned int a = 100;
    unsigned int b = a << 2; // We're NOT type casting to float
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

Output

a = 100, b = 400

Indeed, we were able to verify that left shifting gives us this result!

Now, let’s look at the right shift operator now.


Using the Right Shift Operator (>>)

This is similar to the left shift operator, but shifts the bits to the right.

Here, the last bits that are replaced do not go anywhere. The first bits are replaced with 0 instead.

shifted_value = old_value >> amount;

This is the same as dividing old_value by 2^amount.

Consider the below snippet:

#include <stdio.h>

int main() {
    unsigned int a = 102;
    unsigned int b = 205;

    unsigned int c = a >> 2;
    unsigned int d = b >> 3;

    printf("a = %d, c = %d\n", a, c);
    printf("b = %d, b = %d\n", b, d);
    return 0;
}

Output

a = 102, c = 25
b = 205, b = 25

In the first case, we got c = 102 / (2^2) = 102 / 4 = 25 (nearest integer)

You can verify the result, by writing the bits!

102 = 1100100
102 >> 2 = 0011001 (25 -> Notice the leading bits are replaced with 0)

205 = 11001101
205 >> 3 = 00011001 (25)

When should you use these operators?

The C standard requires the implementation of the arithmetic left and right shifts.

But, the behavior of these operators on signed numbers is not defined by the standard.

This is because there can be overflow errors when left shifting unsigned numbers!

Therefore, make note of the following points whenever you are about to use these operators:

  • Left shifts of negative signed numbers are typically undefined, for most implementations.
  • Unless you require an arithmetic right shift, it’s better if you do the bit shifting only for unsigned types, as you can avoid a possible overflow error.

So my suggestion would be to use them only on unsigned datatypes.


Conclusion

We learned about using the left shift and right shift operators in C / C++, which are useful for performing bit shifting operations on unsigned numbers.

For similar content, do go through our tutorial section on C programming!

References


Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages