This Function can reverse the Bit order in a Byte, Word or a Long, to allow for the reversal of up to 32 Bits at a time. It appers as 3 functions but you can include them all in the code, it will only compile the one that it needs.
FunctionRevBits(BitsInasByte)asByteDimIndexasByteforIndex=0to7RotateBitsInLeft' Shift Left MSB into CarryRotateRevBitsRight' Shift Carry Right into MSB NextEnd FunctionFunction RevBits(BitsIn as Word) as Word Dim Index as Byte for Index = 0 to 15RotateBitsInLeft' Shift Left MSB into CarryRotateRevBitsRight' Shift Carry Right into MSB NextEnd FunctionFunction RevBits(BitsIn as Long) as Long Dim Index as Byte for Index = 0 to 31RotateBitsInLeft' Shift Left MSB into CarryRotateRevBitsRight' Shift Carry Right into MSB NextEnd Function
Here is a short piece of test code to show it in operation.
'
' BitReversal.gcb
'
#chip 16f690,4
dim TestVal as word
TestVal = 0b0000011010100010
' 0b0100010101100000
dir PortC Out
PortC = TestVal_H
wait 1 s
TestVal = RevBits(TestVal)
PortC = TestVal_H
End
I hope you find it of use.
Cheers,
Chris
Last edit: Chris Roper 2017-11-18
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This Function can reverse the Bit order in a Byte, Word or a Long, to allow for the reversal of up to 32 Bits at a time. It appers as 3 functions but you can include them all in the code, it will only compile the one that it needs.
Here is a short piece of test code to show it in operation.
I hope you find it of use.
Cheers,
Chris
Last edit: Chris Roper 2017-11-18