type ubyte4
dim as ubyte hi : 4
dim as ubyte lo : 4
declare sub split(value as ubyte)
declare function unsplit as ubyte
end type
sub ubyte4.split(value as ubyte)
hi = (value & 0xF0)
lo = ((value<<4) & 0xF0)
end sub
dim as ubyte value = &b11110101
dim as ubyte4 s
s.split(value)
print bin(s.hi)
print bin(s.lo)
sleep
Function Low4bit(n As Byte) As Byte
Return n And &h0F
End Function
Function High4bit(n As Byte) As Byte
Return (n And &hF0) Shr 4
End Function
Dim As Byte n = 165
'&hA5
'1010 0101
'10 5
'A 5
? "Hex: " & Hex(n)
?
? "High: " & Hex(High4bit(n))
? "Low: " & Hex(Low4bit(n))
Sleep
Last edited by UEZ on Nov 18, 2020 9:22, edited 1 time in total.
Dim shared As ubyte lo, hi
sub split(value as ubyte)
lo = value and &HF
hi = value shr 4
end sub
dim as ubyte value = &b11110101
split(value)
print bin(value)
print bin(hi);" ";bin(lo)
sleep
union Nibbles
as ubyte value
type
lo : 4 as ubyte
hi : 4 as ubyte
end type
end union
dim as Nibbles n = ( &b11110101 )
? bin( n.value, 8 )
? bin( n.hi, 4 )
? bin( n.lo, 4 )
sleep()
Type Byte8bitSplit
Public:
Declare Constructor (Byval b As Byte = 0)
Declare Operator Let (Byval b As Byte)
Declare Operator [] (Byval n As Integer) As Byte
Private:
Union
Dim As Byte value
Type
Dim As Byte lo : 4
Dim As Byte hi : 4
End Type
End Union
End Type
Constructor Byte8bitSplit (Byval b As Byte = 0)
This.value = b
End Constructor
Operator Byte8bitSplit.Let (Byval b As Byte)
This.value = b
End Operator
Operator Byte8bitSplit.[] (Byval n As Integer) As Byte
Select Case As Const n
Case 0 '' Byte 8bit
Return This.value
Case 1 '' Low 4bit
Return This.lo
Case 2 '' High 4bit
Return This.hi
End Select
Return 0
End Operator
'------------------------------------------------------------
Dim As Byte8bitSplit b = &b10100101
Print "Byte 8bit: "; Bin(b[0], 8)
Print "High 4bit: "; Bin(b[2], 4)
Print "Low 4bit: "; Bin(b[1], 4)
Print
b = &b01111110
Print "Byte 8bit: "; Bin(b[0], 8)
Print "High 4bit: "; Bin(b[2], 4)
Print "Low 4bit: "; Bin(b[1], 4)
Print
Print "Byte 8bit: "; Bin(Byte8bitSplit(&b01101101)[0], 8)
Print "High 4bit: "; Bin(Byte8bitSplit(&b01101101)[2], 4)
Print "Low 4bit: "; Bin(Byte8bitSplit(&b01101101)[1], 4)
Print
Sleep
Function mkulongint(n1 As Ulong,n2 As Ulong) As Ulongint
Dim As Ulongint res
Cast(Ulong Ptr,@res)[0]=n1
Cast(Ulong Ptr,@res)[1]=n2
Return res
End Function
Sub getulongs(n As Ulongint, Byref L1 As Ulong,Byref L2 As Ulong)
L1=Cast(Ulong Ptr,@n)[0]
L2=Cast(Ulong Ptr,@n)[1]
End Sub
Dim As Ulong L1,L2
Dim As Ulongint u
do
Print
l1=rnd*4294967295
l2=rnd*4294967295
Print hex(l1),hex(l2),"two ulongs (4 bits each)"
u=mkulongint(l1,l2)
Print hex(u),"one ulongint (8 bits)"
l1=0
l2=0
getulongs(u,L1,L2)
Print hex(L1),hex(L2),"Return values from the ulongint : two ulongs (4 bits each)"
print
print "Press any key or <escape> to finish"
Sleep
loop until inkey=chr(27)
#define mk(a,b) a or b shl 8
dim as byte a,b
do
a=rnd*128
b=rnd*128
var m=mk(a,b)
print hex(m)
print hex(lobyte(m)),hex(hibyte(m))
print
print "Press any key or <escape> to finish"
sleep
loop until inkey=chr(27)
Sometimes letting a compiler do the bit masking is just easier than doing it manually. I remember implementing the FAT12 file system many years ago for a uni class. In the FAT12 file system, clusters were stored as a 12-bit field, meaning 3 bytes contained two clusters, which were stored as 2 12-bit fields, but the two clusters were stored together as a 3-byte, little-endian number for whatever reason. Whoever heard of using little endian to store a 3-byte long number? Bizarre choice, but one a compiler can deal with easily. Anyway picking the bits out manually is possible, but it's way easier to use a structure with bit fields.
dim as ubyte value = &b1010110
print bin(value)
print bin(value shr 4);" ";bin(value and &HF) ' simple, basic code (pun intended)
#define loNibble(x) (x And 15) ' for those who like
#define hiNibble(x) (x shr 4) ' it very complicated
print bin(hiNibble(value));" ";bin(loNibble(value))
sleep
#define loNibble(x) (x And 15)
#define hiNibble(x) ((x And 240) shr 4)
#define mk(a,b) a or b shl 4
dim as ubyte b,nh,nl
do
b=rnd*255
print "Hex number = "; hex(b),,"(number =";b;")"
nh=hinibble(b)
nl=lonibble(b)
print "High ";hex(nh),"low ";hex(nl)
print "Return hex:"
print hex(mk(nl,nh))
print
print "Press any key or <escape> to finish"
print
sleep
loop until inkey=chr(27)
The best solution is probably the union method,
Thanks MrSwiss for pointing out the stray ushort.