June 2017
Beginner
502 pages
11h 26m
English
The bitwise right shift is a great method to divide a number by 2 at each position, so division by power of 2, shifting the bit to the right. Note that this operation pads on the left with the most significant bit, that is the sign bit, so everything will be padded by one, but the following example will make everything easier:
zarrelli:~$ x=160 ; echo $((x>>1))80zarrelli:~$ x=160 ; echo $((x>>2))40zarrelli:~$ x=160 ; echo $((x>>3))20zarrelli:~$ x=160 ; echo $((x>>4))10
So, let's start with 160:
10100000
|
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
1 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
|
128 |
32 |
What we will do now is to shift one position to the right:
1010000
|
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|
0 |
1 |
0 |
Read now
Unlock full access