## [futurebasic] Re: [FB] Bit swapping

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

From: Hans van Maanen <hvmaanen@...>
Date: Fri, 22 Oct 1999 14:22:00 +0200
```Hi --

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:

'Split the byte in half, and make a lookup-table of inverted
'4-bit numbers, e.g. f(7) = 14, because 0111 <> 1110

DIM f(15)
DATA 0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15
FOR i = 0 TO 15
READ f(i)
NEXT

'Then twiddle the bits of the two halves of the byte, e.g. if our byte is a
= 26 = &1A

a  = &1A
b1 = a  /  16               'b1 = &1
b2 = a MOD 16               'b2 = &A
c  = 16 * f(b2) + f(b1)              ' c = 16 * 5 + 8 = &58

'Which gives the reversed byte in c.

How's that? It seems about midway between the two beautiful suggestions
Robert Purves made, doesn't it?
Now, there should be a way to find the reverses without recourse to a
lookup-table...

Thanks all for input,

Hans van Maanen

```