Byte Me

Published:

Updated:

Module MyFunctions

 

    '------------------------------------------------------------

    'Enumerated Bit States for CSTEBit function

    'Used with CSTEBit function

    'Example:

    '    'This will change the value of MyByte from &HF0 to &HF8

    '    Dim MyByte As Byte = &HF0

    '    Dim ChangeThisBit As Byte = &H04

    '    MyByte = CSTEBit(BitState.SetBit, MyByte, ChangeThisBit)

    '------------------------------------------------------------

 

    Enum BitState As Byte

        ClrBit = 0 'Clear bit

        SetBit = 1 'Set bit

        TogBit = 2 'Toggle bit

        ExmBit = 3 'Examine bit

    End Enum

 

    '----------------------------------------------------------------------

    'Function for changing and examining bit states within a byte

    'A byte has 8 bits to Set or Clear and each position is numbered 0 to 7

    'Each position of the bits is as follows:

    'Position - 7 6 5 4 3 2 1 0

    '     Bit - 1 0 1 0 1 1 0 1

    '          ^ ^ ^ ^ ^ ^ ^ ^

    ' Position  | | | | | | | |

    '   Values  | | | | | | | |

    '    128---+ | | | | | | |

    '     64-----+ | | | | | |

    '     32-------+ | | | | |

    '      16---------+ | | | |

    '      8-----------+ | | |

    '      4-------------+ | |

    '      2---------------+ |

    '      1-----------------+

    '-----------------------------------------------------------------------

 

    Public Function CSTEBit(ByVal CSTE As Byte, ByVal Byt As Byte, ByVal Bit As Byte) As Byte

 

        If Bit < 8 Then

 

            'Bit positon mask

            Dim Mask As Byte = 2 ^ Bit

 

            Select Case CSTE

 

                Case BitState.ExmBit

 

                    'Examine bit

                    'Returns a byte value of &H01 for set and &H00 for clear

                    If ((Byt And Mask) > 0) Then

 

                        CSTEBit = 1

 

                    Else

 

                        CSTEBit = 0

 

                    End If

 

                Case BitState.ClrBit

 

                    'Clear bit 

                    CSTEBit = Byt And Not Mask

 

                Case BitState.TogBit

 

                    'Toggle bit

                    CSTEBit = Byt Xor Mask

 

                Case BitState.SetBit

 

                    'Set bit

                    CSTEBit = Byt Or Mask

 

                Case Else

 

                    'Return same byte if CSTE out of bounds

                    CSTEBit = Byt

 

            End Select

 

        Else

 

            'Return same byte if Bit is out of bounds

            CSTEBit = Byt

 

        End If

 

    End Function

 

End Module

Entry #431

Comments

Avatar Todd -
#1
Isn't bit manipulation great? (And efficient?)

It's a highly subjective topic, but I had a couple of comments to throw out there for your consideration.

First thing is that I've had to write similar multi-purpose functions, but over the years I've tended to break them into multiple functions -- one function per operation type. So if I were writing similar code today, I'd probably make four separate functions -- such as bitExamine(), bitClear(), bitToggle(), and bitSet(). (All start with "bit" so they sort & group together in IntelliSense.)

The other thing that helps in every version of VB after 6.0 is to use "Return" with a function, rather than setting the function name variable. So if you were going to have a Select Case structure, I would put the Return right into the Case, like:

Case BitState.CleBit
    Return Byt And Not Mask

Avatar JADELottery -
#2
Yeah, I was going to do that, but I wanted to keep the compatibility with VB6 just in case there were still some VB6 developers out there.

Great suggestion though.

Post a Comment

Please Log In

To use this feature you must be logged into your Lottery Post account.

Not a member yet?

If you don't yet have a Lottery Post account, it's simple and free to create one! Just tap the Register button and after a quick process you'll be part of our lottery community.

Register