Resources | Subject Notes | Computer Science
This section explores logical binary shifts on 8-bit positive integers. We will understand how these shifts affect the binary representation of a number.
Logical shifts are a fundamental way to manipulate binary data. There are two main types of logical shifts: left shift and right shift.
A logical right shift moves the bits to the right by a specified number of positions. Empty positions on the left are filled with zeros. This is often used for division by powers of 2.
Consider a positive 8-bit binary integer. When we perform a logical right shift, the bits are shifted one position to the right. The rightmost bit is discarded, and a zero is inserted at the leftmost position.
The formula for a logical right shift is:
$N >> n$
where N is the number and n is the number of positions to shift.
A logical left shift moves the bits to the left by a specified number of positions. Empty positions on the right are filled with zeros. This is often used for multiplication by powers of 2.
When we perform a logical left shift, the bits are shifted one position to the left. The leftmost bit is discarded, and a zero is inserted at the rightmost position.
The formula for a logical left shift is:
$N << n$
where N is the number and n is the number of positions to shift.
Let's consider an example with an 8-bit binary integer.
Original Number: 10110011 (decimal equivalent: 179)
Logical Right Shift by 2 positions:
Original: 10110011
Shifted: 00101100 (decimal equivalent: 44)
Logical Left Shift by 3 positions:
Original: 10110011
Shifted: 0011001100 (decimal equivalent: 224)
Operation | Binary Representation | Decimal Equivalent |
---|---|---|
Logical Right Shift by 1 | 10110011 → 00101100 | 179 → 44 |
Logical Left Shift by 1 | 10110011 → 01011001 | 179 → 358 |
Logical Right Shift by 2 | 10110011 → 00101100 | 179 → 44 |
Logical Left Shift by 2 | 10110011 → 0101100100 | 179 → 716 |
Performing logical shifts on binary integers allows us to efficiently perform operations like division and multiplication by powers of 2. The choice between left and right shifts depends on the desired operation.
Right shifts are used for division by powers of 2, discarding the remainder. Left shifts are used for multiplication by powers of 2, effectively adding zeros to the end of the number.