Right shift (>>)

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))
80
zarrelli:~$ x=160 ; echo $((x>>2))
40
zarrelli:~$ x=160 ; echo $((x>>3))
20
zarrelli:~$ 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

1

0

0

0

0

64

1

 

Now we have 80, but again, one position to the left:

128

64

32

16

8

4

2

1

0

0

1

0

1

0

0

0

32

8

 

Now, as an exercise, calculate by yourself the remaining values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.22.135