C Left Shift & Right Shift Operations Explained
In the C language, left shift and right shift are bitwise operators used to move the binary digits of a number. The left shift operator << moves the binary digits of a number to the left by a specified number of positions, filling in empty spaces on the right with zeros. The right shift operator >> moves the binary digits of a number to the right by a specified number of positions, filling in the left spaces according to the sign bit.
The specific rules are as follows:
The left shift operator <<: shifting a number to the left by n bits is equivalent to multiplying it by 2^n. For example, a << 2 means shifting a to the left by 2 bits, which is equivalent to multiplying a by 4.
Right shift operator >>: Shifting a number to the right by n places is equivalent to dividing it by 2 to the power of n. If the operand is an unsigned integer, the vacant bits are filled with 0; if the operand is a signed integer, the vacant bits are filled with the sign bit (thus keeping the sign unchanged).