IntSplit() Macro "all in one" (whole numbers splitter)

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

IntSplit() Macro "all in one" (whole numbers splitter)

Postby MrSwiss » Jun 28, 2019 15:56

Hi,

using the advantage, that inputs & outputs of a Macro are "type-less".
The Macro itself determines the SizeOf(input) and calls the correct
part of the required splitting algo (and, of course, the return type) ...
(Int referres to any fixed/dynamic sized integer-data-type!)
FBC 64 bit only! (probably works in 32 bit also, but plenty warnings)

IntSplit_Mac-test1.bas:

Code: Select all

' IntSplit_Mac-test1.bas -- (c) 2019-06-28, MrSwiss
'
' compile: -s console
'

#Macro IntSplit(in_, low_, high_)       ' like a sub with 2 byref returns
    Select Case As Const SizeOf(in_)    ' determine size of input variable
        Case 8                          ' LongInt/ULongInt (input)
            high_ = CULng(in_ Shr 32)   ' returns: 2 Ulong
            low_  = CULng(in_ And &h00000000FFFFFFFF)
        Case 4                          ' Long/ULong (input)
            high_ = CUShort(in_ Shr 16) ' returns: 2 UShort
            low_  = CUShort(in_ And &h0000FFFF)
        Case 2                          ' Short/UShort (input)
            high_ = CUByte(in_ Shr 8)   ' returns: 2 UByte
            low_  = CUByte(in_ And &h00FF)
        Case 1                          ' Byte/UByte (input)
            high_ = CUByte(in_) Shr 4   ' returns: 2 Nibbles (as UByte)
            low_  = CUByte(in_) And &h0F
    End Select
#EndMacro


Dim As LongInt  start = &h123456789ABCDEF0
Dim As Long     hi32, lo32
Dim As Short    sh(3)
Dim As Byte     by(7), ni(15)

Print Hex(start, 16); "   original, split it ..." : Print

IntSplit(start, lo32, hi32)             ' split 64 bit's
Print Hex(hi32, 8), Hex(lo32, 8)
print

IntSplit(lo32, sh(3), sh(2))            ' split 32 bit's
IntSplit(hi32, sh(1), sh(0))
Print Hex(sh(0), 4), Hex(sh(1), 4), Hex(sh(2), 4), Hex(sh(3), 4)
Print

IntSplit(sh(0), by(7), by(6))           ' split 16 bit's
IntSplit(sh(1), by(5), by(4))
IntSplit(sh(2), by(3), by(2))
IntSplit(sh(3), by(1), by(0))
Print Hex(by(0), 2), Hex(by(1), 2), Hex(by(2), 2), Hex(by(3), 2); "   Line 1 (byte)"
Print Hex(by(4), 2), Hex(by(5), 2), Hex(by(6), 2), Hex(by(7), 2); "   Line 2"
Print

IntSplit(by(0), ni(15), ni(14))         ' split byte to nibble's
IntSplit(by(1), ni(13), ni(12))
IntSplit(by(2), ni(11), ni(10))
IntSplit(by(3), ni(9),  ni(8))
IntSplit(by(4), ni(7),  ni(6))
IntSplit(by(5), ni(5),  ni(4))
IntSplit(by(6), ni(3),  ni(2))
IntSplit(by(7), ni(1),  ni(0))
Print Hex(ni(0), 2),  Hex(ni(1), 2),  Hex(ni(2), 2),  Hex(ni(3), 2);  "   Line 1 (nibble)"
Print Hex(ni(4), 2),  Hex(ni(5), 2),  Hex(ni(6), 2),  Hex(ni(7), 2);  "   Line 2"
Print Hex(ni(8), 2),  Hex(ni(9), 2),  Hex(ni(10), 2), Hex(ni(11), 2); "   Line 3"
Print Hex(ni(12), 2), Hex(ni(13), 2), Hex(ni(14), 2), Hex(ni(15), 2); "   Line 4"

Locate csrlin + 2
Print "... done ... ";
Sleep

Return to “Tips and Tricks”

Who is online

Users browsing this forum: MSN [Bot] and 1 guest