[futurebasic] Re: [FB] Bit swapping

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 1999 : Group Archive : Group : All Groups

From: Robert Purves <robert.purves@...>
Date: Sat, 23 Oct 1999 09:10:43 +1300
>I remember asking...
>
>>
>>>What would be the fastest way to swap eight bits, so that e.g. 64 becomes 2
>>>(01000000 <> 00000010) and 192 becomes 3 (11000000 <> 00000011)?
>>
>
>... and then I thought:

And then I, too, thought:  "What about BetterBitRev?"

Robert Purves

'---------------A complete FB2 or FB^3 program----------------
END GLOBALS

' register on' uncomment for FB^3

LOCAL FN BetterBitRev(num&, nBits)
DIM out&,j
out& = 0
FOR j = 1 TO nBits
out& = (out&<<1) OR (num& AND 1)
num& = num&>>1
NEXT
END FN = out&

DIM b&, rb&
WINDOW 1,"",(0,0)-(620,300)

DEFSTR LONG
' test 8-byte reversal
b&=123: rb& = FN BetterBitRev(b&, 8)
PRINT "8-bit " RIGHT$(BIN$(b&),8) " --> " RIGHT$(BIN$(rb&),8)

' test 16-bit reversal
b&=12301: rb&=FN BetterBitRev(b&, 16)
PRINT "16-bit "RIGHT$(BIN$(b&),16) " --> " RIGHT$(BIN$(rb&),16)

' test 32-bit reversal
b&=-1230000101: rb&=FN BetterBitRev(b&, 32)
PRINT "32-bit " RIGHT$(BIN$(b&),32) " --> " RIGHT$(BIN$(rb&),32)

DO: HANDLEEVENTS: UNTIL FN BUTTON
'----------------------------------------------------