Chris Roper - 2017-11-18

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.

Function RevBits(BitsIn as Byte) as Byte
  Dim Index as Byte
  for Index = 0 to 7
    Rotate BitsIn  Left    ' Shift Left MSB into Carry
    Rotate RevBits Right   ' Shift Carry Right into MSB
  Next
End Function

Function RevBits(BitsIn as Word) as Word
  Dim Index as Byte
  for Index = 0 to 15
    Rotate BitsIn  Left    ' Shift Left MSB into Carry
    Rotate RevBits Right   ' Shift Carry Right into MSB
  Next
End Function

Function RevBits(BitsIn as Long) as Long
  Dim Index as Byte
  for Index = 0 to 31
    Rotate BitsIn  Left    ' Shift Left MSB into Carry
    Rotate RevBits Right   ' Shift Carry Right into MSB
  Next
End 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