Rotate vs Shift
#4
(09-08-2022, 10:47 PM)TDarcos Wrote: Shift is like a bulldozer pushing the bits off the edge. The bits are replaced by 0 for non-negative numbers and 1 for negative numbers.

Rotate is like putting the bits on a conveyor belt or a lazy Susan. the bits that roll off come back around.

That's a great clarification that I either missed or wasn't stated elsewhere. A right shifted negative number will end at -1, the Most Significant Bits (MSB) being filled by 1s. I'll have to play with the action for _UNSIGNED types when I have more time.

Code: (Select All)
a% = -20
PRINT _BIN$(a%), a%
a% = _SHR(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHR(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHR(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHR(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHR(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHR(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHR(a%, 2)
PRINT _BIN$(a%), a%

I also note that with a negative value, it will put the 1s on the MSB and 0s on the LSB, depending on the shift direction. Which keeps the action working as a multiple of 2, regardless of the shift direction. Enough left shifts of a negative will, however, overflow the variable type container to 0.

Code: (Select All)
a% = -1
PRINT _BIN$(a%), a%
a% = _SHL(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHL(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHL(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHL(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHL(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHL(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHL(a%, 2)
PRINT _BIN$(a%), a%
a% = _SHL(a%, 2)
PRINT _BIN$(a%), a%
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply


Messages In This Thread
Rotate vs Shift - by SMcNeill - 09-05-2022, 11:22 PM
RE: Rotate vs Shift - by OldMoses - 09-05-2022, 11:37 PM
RE: Rotate vs Shift - by TDarcos - 09-08-2022, 10:47 PM
RE: Rotate vs Shift - by OldMoses - 09-11-2022, 12:51 PM



Users browsing this thread: 3 Guest(s)