If I have 2 hex digits (0-15) as the nibbles of one byte, what's the fastest way to retrieve those two numbers from that byte value? (I.E. 255 to 15 and 15)
Thanks.
Separating Hex Values
Re: Separating Hex Values
Ok, let's call your original byte as BYTE.BDZ wrote:If I have 2 hex digits (0-15) as the nibbles of one byte, what's the fastest way to retrieve those two numbers from that byte value? (I.E. 255 to 15 and 15)
Thanks.
For this to work, you must do:
DIM BYTE AS INTEGER
To isolate the rightmost nibble, just remove the leftmost 4 bits by "anding" them out, like this:
RIGHTNIBBLE = BYTE AND 240
or, if you rather work with hex,
RIGHTNIBBLE = BYTE AND &HF0
To isolate the leftmost nibble, you can simulate shifting the left nibble 4 bits into the rightmost (low order) 4 bit positions by dividing the byte by 16. Dividing by 16 is the same as dividing by 2^4.
LEFTNIBBLE = BYTE / 16
*****
Code: Select all
LEFTNIBB = BYTE \ 16
RIGHTNIBB = BYTE MOD 16
You can replace the MOD 16 with AND 15 to gain a clock cycle or two per call.. (Note: If using FB this is done automatically, using MOD or AND doesent matter as the compiler optimize MOD n where n is a power of 2 to AND n-1)
Also, by using integer division (\) instead of floating division (/) we gain even more clock cycles as the equation ignores any decimals during evaluation..
I have left this dump.