Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bite operations with ShL and ShR
#1
This demo doesn't focus on reading Byte directly, but shows how to do the same job more easily using _ShL and ShR bit shifting. It took me a while to figure out how to use it, but I thought we could share our experiences with reading bit values from Byte here.

Code: (Select All)

'_ShL, _ShR - how place info to byte and then read it back

GameTyp = 2 ' is needed 2 bites
MaskTyp = 6 ' is needed 4 bites
TimeH = 20 '  is needed 5 bites
TimeM = 44 '  is needed 6 bites
TimeS = 33 '  is needed 6 bites


Print "writing to bytes this values:"
Print "Seconds:"; TimeS
Print "Minutes:"; TimeM
Print "Hours:"; TimeH
Print "Mask:"; MaskTyp
Print "GameTyp:"; GameTyp

Dim msk As Long '32 bite, but here is used 23 bites only


'why GameTyp bite lenght is 2?          Is needed here store values 0 to 3. For this are 2 bites enought, in two bites can be stored values 0, 1, 2, 3
'why MaskTyp bite lenght is 4?          Is needed here store 16 values. To 4 bite can be stored values 0 to 15 (16 values total)
'why TimeH bite lenght is 5?            TimeH is for saving hours. To 5 bites can be stored 32 values (0 to 31)
'why TimeM and TimeS bite lenght is 6?  TimeM save minutes and TimeS save seconds. Both are values 0 to 60. To 6 bites can be stored value 0 to 63.


'output 4 bytes structure (LONG is 4 byte value):

'    Variable          GameTyp  MaskTyp  TimeH  TimeM  TimeS
'    Bite Lenght          2      4        5      6      6
'Position in BYTE:      23      21      17    12      6

'write values to Bytes:
msk = msk Or _ShL(GameTyp, 21) '      it is always necessary to shift the bit value to the position as it will be stored in the byte (see above)
msk = msk Or _ShL(MaskTyp, 17)
msk = msk Or _ShL(TimeH, 12)
msk = msk Or _ShL(TimeM, 6)
msk = msk Or TimeS

msk2 = msk
_ControlChr Off
Print
Print "All values writed as"; msk; "("; MKL$(msk); ")"
Print
Print "Reading values from this bytes"
'now - read values back from created msk value:

TimeSec = msk And 63 ' read 6 bites    (63 is 00111111)
msk = _ShR(msk, 6) '  shift bites right up 6 bite
TimeMin = msk And 63 ' read 6 bites
msk = _ShR(msk, 6) '  right bites up 6 bite
TimeHrs = msk And 31 ' read 5 bites    (31 is 00011111)
msk = _ShR(msk, 5) '  shift bites right up 5 bites
Mtyp = msk And 15 '    read 4 bites    (15 is 00001111)
msk = _ShR(msk, 4) '  shift bites right up 4 bites
Gtyp = msk And 3 '    read 2 bites    (3 is 00000011)

Print "Seconds:"; TimeSec
Print "Minutes:"; TimeMin
Print "Hours:"; TimeHrs
Print "Mask:"; Mtyp
Print "GameTyp:"; Gtyp


Reply


Messages In This Thread
Bite operations with ShL and ShR - by Petr - 11-16-2024, 08:44 PM
RE: Bite operations with ShL and ShR - by Petr - 11-16-2024, 10:28 PM
RE: Bite operations with ShL and ShR - by Pete - 11-17-2024, 01:31 AM
RE: Bite operations with ShL and ShR - by Pete - 11-19-2024, 01:03 AM



Users browsing this thread: 1 Guest(s)