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
#2
Its good code.    And I've been playing with similar techniques to pack RAM more tightly on the X16 a neo-retro 8 bit computer.    However,  I can't see why would ever need this technique on even a barely modern system.    It's seems kind of a waste for a relatively small RAM savings.     

   No offense meant.   It is quite nice code actually.    IMO I just don't see the point.   Wink.
Reply
#3
(11-16-2024, 09:34 PM)ahenry3068 Wrote: Its good code.    And I've been playing with similar techniques to pack RAM more tightly on the X16 a neo-retro 8 bit computer.    However,  I can't see why would ever need this technique on even a barely modern system.    It's seems kind of a waste for a relatively small RAM savings.     

   No offense meant.   It is quite nice code actually.    IMO I just don't see the point.   Wink.

What's the point? Huge. Just read the documentation for MP3 or MP4 format, for GIF format, also for TTF font files, for 1-bit bitmaps and so on. Bit fields need to be decoded everywhere, it's a perfectly common practice.


Reply
#4
(11-16-2024, 09:34 PM)ahenry3068 Wrote: Its good code.    And I've been playing with similar techniques to pack RAM more tightly on the X16 a neo-retro 8 bit computer.    However,  I can't see why would ever need this technique on even a barely modern system.    It's seems kind of a waste for a relatively small RAM savings.     

   No offense meant.   It is quite nice code actually.    IMO I just don't see the point.   Wink.

The point is to use a variable for bit-packing.   In this case though, I personally think I'd just use a custom type and save myself a lot of shifting and headache.

TYPE msl_type
    game as _byte
    mask as _byte
    hour as _byte
    minute as _byte
    second as _byte
END TYPE

5 bytes with each to use, access, and something everyone can understand at a glance.   Compared to 4 bytes which is rather complex for a lot of folks to ever sort out and work with.

Trust me, I can promise you which one is easiest to come back to and work on after a 2 year break or whatnot. Smile

(11-16-2024, 10:28 PM)Petr Wrote:
(11-16-2024, 09:34 PM)ahenry3068 Wrote: Its good code.    And I've been playing with similar techniques to pack RAM more tightly on the X16 a neo-retro 8 bit computer.    However,  I can't see why would ever need this technique on even a barely modern system.    It's seems kind of a waste for a relatively small RAM savings.     

   No offense meant.   It is quite nice code actually.    IMO I just don't see the point.   Wink.

What's the point? Huge. Just read the documentation for MP3 or MP4 format, for GIF format, also for TTF font files, for 1-bit bitmaps and so on. Bit fields need to be decoded everywhere, it's a perfectly common practice.

It's also not just for modern formats.  Even old-school graphics did this with bit packing for black/white screens.   Screen 7(I think) packs it's memory into 4-bit nibbles (half-bytes).   It's not rare at all to see this type of thing in use with more advanced code and projects.  Smile
Reply
#5
(11-16-2024, 10:30 PM)SMcNeill Wrote:
(11-16-2024, 09:34 PM)ahenry3068 Wrote: Its good code.    And I've been playing with similar techniques to pack RAM more tightly on the X16 a neo-retro 8 bit computer.    However,  I can't see why would ever need this technique on even a barely modern system.    It's seems kind of a waste for a relatively small RAM savings.     

   No offense meant.   It is quite nice code actually.    IMO I just don't see the point.   Wink.

The point is to use a variable for bit-packing.   In this case though, I personally think I'd just use a custom type and save myself a lot of shifting and headache.

TYPE msl_type
    game as _byte
    mask as _byte
    hour as _byte
    minute as _byte
    second as _byte
END TYPE

5 bytes with each to use, access, and something everyone can understand at a glance.   Compared to 4 bytes which is rather complex for a lot of folks to ever sort out and work with.

Trust me, I can promise you which one is easiest to come back to and work on after a 2 year break or whatnot. Smile

(11-16-2024, 10:28 PM)Petr Wrote:
(11-16-2024, 09:34 PM)ahenry3068 Wrote: Its good code.    And I've been playing with similar techniques to pack RAM more tightly on the X16 a neo-retro 8 bit computer.    However,  I can't see why would ever need this technique on even a barely modern system.    It's seems kind of a waste for a relatively small RAM savings.     

   No offense meant.   It is quite nice code actually.    IMO I just don't see the point.   Wink.

What's the point? Huge. Just read the documentation for MP3 or MP4 format, for GIF format, also for TTF font files, for 1-bit bitmaps and so on. Bit fields need to be decoded everywhere, it's a perfectly common practice.

It's also not just for modern formats.  Even old-school graphics did this with bit packing for black/white screens.   Screen 7(I think) packs it's memory into 4-bit nibbles (half-bytes).   It's not rare at all to see this type of thing in use with more advanced code and projects.  Smile
  Ahh.  Ok I stand corrected.   I just didn't get the context for the code.      I didn't think of decoding packed file formats at all.   I've been using bit packed variables to save memory and that's what I thought of first...     I'll think twice next time.
Reply
#6
No worries. A lot of folks think my routines are worthless until I come back to explain them and prove them to be worthless.

Pete Big Grin

- I don't give two shifts about a bit.
Reply
#7
(11-17-2024, 01:31 AM)Pete Wrote: No worries. A lot of folks think my routines are worthless until I come back to explain them and prove them to be worthless.

Pete Big Grin

- I don't give two shifts about a bit.

I bet my routines are worthlesser than your routines!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#8
Ya mean MORE worthlesser, varmint! Big Grin

Howdy ya xpect folks who visit here ta larn Inglish?

 - Sam
Shoot first and shoot people who ask questions, later.
Reply




Users browsing this thread: 1 Guest(s)