When working with C programming language, you may come across different operators and expressions that might seem confusing at first. In this article, we will discuss the difference between (b<<2) >>2 and b & 0x3F expressions in C, and how they can be used in your code.
Bitwise Left Shift and Right Shift Operators
Before diving into the differences, let's understand the bitwise left shift (<<) and bitwise right shift (>>) operators in C. These operators allow you to shift the bits of a number to the left or right, respectively.
The bitwise left shift operator (<<) moves the bits to the left by a specified number of positions. For example, if we have the number 5 represented in binary as 00000101, shifting it left by 2 positions would result in 00010100. The vacant positions are filled with zeroes.
The bitwise right shift operator (>>) moves the bits to the right by a specified number of positions. For example, if we have the number 20 represented in binary as 00010100, shifting it right by 2 positions would result in 00000101. Again, the vacant positions are filled with zeroes.
(b<<2) >>2
Now, let's discuss the expression (b<<2) >>2. This expression involves both bitwise left shift and bitwise right shift operators.
When you evaluate (b<<2), the value of b is shifted left by 2 positions. This means that the bits of b are moved two positions to the left, and the vacant positions are filled with zeroes.
Then, when you evaluate >>2, the result of the previous operation is shifted right by 2 positions. This means that the bits are moved two positions to the right, and the vacant positions are filled with zeroes.
The expression (b<<2) >>2 can be used to perform a quick division by a power of two. Since shifting bits to the left is equivalent to multiplying by a power of two, and shifting bits to the right is equivalent to dividing by a power of two, this expression effectively divides b by 4 (since we are shifting twice).
For example, if b is 20, shifting it left by 2 positions would give us 80 (00010100 becomes 01010000). Then, shifting 80 right by 2 positions would give us 20 again (01010000 becomes 00010100).
b & 0x3F
Now, let's move on to the expression b & 0x3F. This expression involves the bitwise AND operator (&) and the hexadecimal value 0x3F.
The bitwise AND operator (&) compares the corresponding bits of two numbers and produces a new number where each bit is set to 1 only if both corresponding bits in the original numbers are 1. Otherwise, the bit is set to 0.
In this expression, b is compared with the hexadecimal value 0x3F. The hexadecimal value 0x3F is equivalent to the binary value 00111111.
When you perform the bitwise AND operation between b and 0x3F, the result is a new number where only the lowest 6 bits of b are preserved, and all other bits are set to 0.
This expression can be useful when you want to extract or mask specific bits from a number. By performing a bitwise AND with a suitable bitmask, you can isolate the desired bits and ignore the rest.
For example, if b is 187 (10111011 in binary), performing b & 0x3F would give us 59 (00111011 in binary).
In summary, the expression (b<<2) >>2 can be used to divide b by 4 (quick division by a power of two), while the expression b & 0x3F can be used to extract or mask specific bits from b. Understanding these expressions and their differences can help you manipulate and extract information from binary data in your C programs.
| Operator/Expression | Description |
|---|---|
(b<<2) >>2 |
Divides b by 4 (quick division by a power of two) |
b & 0x3F |
Extracts or masks specific bits from b |