Packing 4 Integers as ONE BYTE?
By : user2836616
Date : March 29 2020, 07:55 AM
it helps some times Use shift and bitwise OR, then convert to a character to get a "byte": code :
x = chr(a  (b << 1)  (c << 2)  (d << 5))
i = ord(x)
a = i & 1
b = (i >> 1) & 1
c = (i >> 2) & 7
d = (i >> 5) & 7
0000000a
0000000b
00000ccc
00000ddd
0000000a
000000b0
000ccc00
ddd00000
dddcccba
dddcccba
0dddcccb
00dddccc
00000ddd
0000000a
0000000b
00000ccc
00000ddd

Packing 4 Integers as ONE INTEGER?
By : user2836696
Date : March 29 2020, 07:55 AM
hop of those help? Equivalently to my previous answer, but using * and + instead of << and , you can do code :
i = a + 2*b + 4*c + 32*d
a, b, c, d = i % 2, i//2 % 2, i//4 % 8, i//32 % 8

Packing 4 integers into one single [10,10,10,2] integer
By : Pradeep Prajapati
Date : March 29 2020, 07:55 AM

Packing sets of non power of 2 integers
By : Alexey
Date : March 29 2020, 07:55 AM
should help you out I have a set of integers, each with a specific range: , A list of variables with different ranges like this: code :
foo = [1, 5]
bar = [1, 10]
baz = [1, 200]
packed = (foo  1) + 5 * (bar  1) + (5 * 10) * (baz  1)
// extract foo
foo = packed % 5 + 1
// drop foo from packed representation
packed /= 5
// extract bar (which is now the lowest digit in 'packed')
bar = packed % 10 + 1
// drop bar
packed /= 10
// top digit is left over
baz = packed + 1

Efficient packing bytes into Integers?
By : Prof. CAJ Herkstrote
Date : March 29 2020, 07:55 AM

