Q:

Byte shifting / casting VB6

I'm extremely unfamiliar with VB6 so please excuse the rookie question:
I'm attempting to turn a long into it's component bytes. In C it is simple because of the automatic truncation and the bitshift operators. For the life of me I cannot figure out how to do this in VB6.
Attempts so far have all generally looked something like this
sys1 = CByte(((sys & &HFF000000) / 16777216))   ' >> 24
sys2 = CByte(((sys & &HFF0000) / 65536))      ' >> 16 

sys1 and sys2 are declared as Byte and sys is declared as Long
I'm getting a type mismatch exception when I try to do this. Anybody know how to convert a Long into 4 Bytes ??
Thanks

A:

You divide correctly, but you forgot to mask out only the least significant bits.
Supply the word you want to divide into bytes, and the index (0 is least significant, 1 is next, etc.)
Private Function getByte(word As Long, index As Integer) As Byte
    Dim lTemp As Long
    ' shift the desired bits to the 8 least significant
    lTemp = word / (2 ^ (index * 8))
    ' perform a bit-mask to keep only the 8 least significant
    lTemp = lTemp And 255
    getByte = lTemp
End Function

