Bitwise shift operators


#1



Left and right bit shift operations is mathematically equivalent to floor dividing and multiplying by 2 (respectively) for every time you shift.
can anyone elaborate this....
i didn't get it......


Replace this line with your code.


#2

In Python, floor division uses the // operator. Floor division is integer division that yields an integer. This is equivalent to rounding down the result to the nearest integer.

101010 // 10 => 10101
10101 // 10  => 1010

Notice that the second result is the rounded value of 1010.1.

Lets do a couple of examples with 2.

12345 // 2  => 6172

This is the rounded (floor) value of 6172.5

6172 // 2   => 3086
3086 // 2   => 1543
1543 // 2   => 771

The last value is the floor of 771.5

771 // 2    => 385

which is the floor of 385.5.

385 // 2    => 192

which is the floor of 192.5

192 // 2    => 96
96 // 2     => 48
48 // 2     => 24
24 // 2     => 12
12 // 2     => 6
6 // 2      => 3
3 // 2      => 1

which is the floor of 1.5.

1 // 2      => 0

which is the floor or 0.5.

That should help get an idea of what floor division is. The above is an example of

12345 >> 14

We have shifted the number to the right 14 times to reach zero.

>>> x = 12345
>>> print (x)
12345
>>> c = 0
>>> while x:
	c += 1
	x = x >> 1
	print (x)

6172
3086
1543
771
385
192
96
48
24
12
6
3
1
0
>>> print (c)
14
>>> print (12345 >> 14)
0
>>>

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.