Code: Select all
unsigned char Bit_Reverse( unsigned char x )
{
x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
return x;
}
EDIT: I wrote of ROR/ROL because I saw them in an ASM version. But the shifts work fine too. But shifts are byte wide and there is no way (and no advantage) to limit it to a nibble
In my case I also need to invert the logic of the bits with
Code: Select all
x = NOT x
Code: Select all
Dim x as Byte
'x has some arbitrary value
x = FlipBits(x)
x = NOT x
x = Shr(x,4) 'or x=Shl(x,4)
Code: Select all
'reverse the order of the low nibble and invert the logic
'use a temporary byte to accumulate the bits
Dim x as Byte
Dim y as Byte
'x has some arbitrary value
y = 0
if (x and &h01) = 0 then y=y or &h08
if (x and &h02) = 0 then y=y or &h04
if (x and &h04) = 0 then y=y or &h02
if (x and &h08) = 0 then y=y or &h01
x = y