In a logical left shift, the bits are shifted to the left by a specified number of positions.
The leftmost bit (sign bit) is shifted out/discarded, and the vacant position on the right is filled with a zero.
Logical left shifts are primarily used for unsigned integer manipulation and simple bit operations.
10110101 becomes 01101010
In a right shift operation, the bits of a binary number are shifted to the right by a specified number of positions.
Similar to left shifts, new bits are introduced on the left side, and the rightmost bits are discarded. Each right shift effectively divides the number by 2 for each position shifted.
For example, if you right-shift the binary number "1010" by one position, you get "0101," which is equivalent to dividing 1010 by 2^1 (2).
Here you can see a key advantage of storing numbers in binary - division is a simple process.
A logical shift is used in computing and digital systems for several purposes, primarily in bitwise operations. Here’s when you would use it:
Logical shifts are used to manipulate individual bits in a binary number, such as clearing or setting specific bits.
A logical left shift (<<) effectively multiplies a number by 2 for each shift position.
A logical right shift (>>) divides a number by 2 for each shift position (but without sign extension).
What is binary shifting in computer science?