Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading portion of a byte

Status
Not open for further replies.

UncleJack

Programmer
Sep 20, 2001
344
0
0
I need to read the first four bits of a byte. Anyone know a way using VBA to do this?

Uncle Jack
 
Several requests for this (or minor variations lately). In todays world, it is generally considered a 'waste' to use any memory less than a "word" of memory for information storage. the generic rationale is that is requires more memory to sub-divide the memory unit (to retrieve the subdivided values) than to store each element as a "word", which may directly stored or retrieved. It is also much more prone to error, as there are no 'standard' routines to support bit manipulation. the following is offered more as an illustration of HOW the various bit values are defined than an actual manipulation mechanisim.


Code:
Public Function basByteMask(BytIn As Byte, Mask As Byte) As Byte

    'Michael Red 1/9/02
    'As usual, for illustration ONLY.

    'ByteIn is a BYTE (0-255)
    'Mask is a BYTE (0-255)
    'Returns a BYTE (0-255)
    'Note: Values of BIts in the Byte:
    '   (Bit #8) = &H80 = 128  (MSB)
    '   (Bit #7) = &H40 = 64
    '   (Bit #6) = &H20 = 32
    '   (Bit #5) = &H10 = 16
    '   (Bit #4) = &H08 = 08
    '   (Bit #3) = &H04 = 04
    '   (Bit #2) = &H02 = 02
    '   (Bit #1) = &H01 = 01    (LSB)
    '   (Bit #7) = &H00 = 00    (Empty - Would ALWAYS Return 0)

    basByteMask = BytIn And Mask

End Function
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top