Re: how to shift bits in memory
- Posted by DerekParnell (admin) Jan 29, 2011
- 1149 views
unkmar said...
I don't know what all is in that zip file.
I do know that bit-shifts are nothing more than multiply or of integers of powers of 2 in euphoria.
These are not bit rotations. The left does not rotate to the right.
integer test = 6 -- 8-bits of binary 00000110 test *= 2 -- left bit shift by 1, 00001100 test *= 4 -- left bit shift by 2, 00110000 test /= power(2, 3) -- power(2,3) = 8, right bit shift by 3, 00000011 test /= 2 -- right bit shift by 1, 00000001
Lucius L. Hilley III - unkmar
It is not quite that simple when 'shifting' negative numbers.
For example, the number -1 has the bit pattern 11111111_11111111_11111111_11111111 and if you wish to shift that two bits to the right you can't simply multiply it by 4.
-4 is 11111111_11111111_11111111_11111100 -1 >> 2 is 10111111_11111111_11111111_11111111