JustPaste.it

Public Class Form1

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

'This routine takes a byte value and re-orders the bits. Effectively the bits are flipped.
' D0 -> D7
' D1 -> D6
' D2 -> D5
' D3 -> D4
' D4 -> D3
' D5 -> D2
' D6 -> D1
' D7 -> D0

Dim originalByte As Byte
Dim flippedByte As Byte
Dim origBitIndex As Byte
Dim convBitIndex As Byte

flippedByte = 0
convBitIndex = 7
originalByte = txtByte.Text 'load original byte value

For origBitIndex = 0 To 7 'Cycle through all 8 bit locations in the Byte

If (BitMask(origBitIndex) And originalByte) Then 'Check if the bit location is a 1
flippedByte = (flippedByte Or BitMask(convBitIndex)) 'If so, set the correct bit in the flipped Byte
End If

If (convBitIndex > 0) Then
convBitIndex = convBitIndex - 1 'Decrease the bit index of the flipped Byte
End If

Next


txtNewByte.Text = flippedByte 'This is the flipped Byte resulting value

 

End Sub


Function BitMask(ByVal index As Byte) As Byte

'This function will return a Bit Mask value, based on the index which was sent

Select Case index
Case 0
BitMask = &H1
Case 1
BitMask = &H2
Case 2
BitMask = &H4
Case 3
BitMask = &H8
Case 4
BitMask = &H10
Case 5
BitMask = &H20
Case 6
BitMask = &H40
Case 7
BitMask = &H80

End Select

End Function


End Class