Logical Binary Shifts |
A binary shift moves all of the bits in a given binary number either to the left or the right by a given number of places. All of the empty spaces are then filled with zeros. A shift of one place to the left will have the following effect: You can move “bits” either to the right or the left
|
In coding the bit shift operators are: << to move to the left and >> to move to the right.
Each of these will be followed by a number so that >>1 means to move one place to the right and << 2 means move to places to the left.
Complete the questions below - all answers in binary:
|
Effects of logical shifts - multiplication and division by powers of 2 | ||||||||||||||||||||||||||||||||
If a binary number is shifted to the left this is equivalent to multiplying the number by 2 for each shift to the left. Two shifts would therefore multiply a number by 4. E.g.
Two places to the left we get the binary number:
If a binary number is shifted to the right, this is equivalent to dividing the number by 2 for each shift to the right.
Three places to the right we get the binary number:
|
Effects of logical shifts - using odd numbers | ||||||||||||||||
When moving the decimal number 17 one place left, it becomes 170 and has therefore been multiplied by its base of 10. An issue with precision occurs where odd numbers are divided since a byte cannot represent fractional numbers. Consider the following shift of three places to the right.
The original binary value was equal to decimal 165. A right shift should divide this by 8 (or divide 2, three times). 165/8 = 20.625. However, the resulting binary converted to decimal is 20. |
Questions |
00110101
The number gets divided by 4 the original number was 214 / 4 = 53
10101100 timesing by 2 the number is 214 x 2 = 428
203
0011 0010
0011 0010 Divided by 4 the original number is 203 / 4 = 50.75 however we lose precision 0000 1100 = 50 instead of 50.75 |