Created
February 28, 2021 16:54
-
-
Save roobie/6ad6142a2625b83b56defacd58810647 to your computer and use it in GitHub Desktop.
bitsnbobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local bit = require("bit") | |
local function u8_ToBinaryRepresentation (u8) | |
local function AND (x, v) | |
local res = bit.band(x, v) | |
if res ~= 0 then | |
return 1 | |
else | |
return 0 | |
end | |
end | |
return table.concat( | |
{ | |
table.concat { | |
AND(u8, 0x80); | |
AND(u8, 0x40); | |
AND(u8, 0x20); | |
AND(u8, 0x10); | |
AND(u8, 0x08); | |
AND(u8, 0x04); | |
AND(u8, 0x02); | |
AND(u8, 0x01); | |
}; | |
}, " ") | |
end | |
local function u32_ToBinaryRepresentation (u32) | |
local AND = bit.band | |
local RSH = bit.rshift | |
local bytes = { | |
RSH(AND(u32, 0xff000000), 24); | |
RSH(AND(u32, 0x00ff0000), 16); | |
RSH(AND(u32, 0x0000ff00), 8); | |
AND( u32, 0x000000ff); | |
} | |
local binaries = { | |
u8_ToBinaryRepresentation(bytes[1]); | |
u8_ToBinaryRepresentation(bytes[2]); | |
u8_ToBinaryRepresentation(bytes[3]); | |
u8_ToBinaryRepresentation(bytes[4]); | |
} | |
return table.concat(binaries, " ") | |
end | |
for i = 0, 31 do | |
local v = bit.lshift(1, i) | |
print(string.format("1 << %d", i), | |
string.format("%s", bit.tohex(v)), | |
string.format("%-12d", v), | |
u32_ToBinaryRepresentation(v)) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 << 0 00000001 1 00000000 00000000 00000000 00000001 | |
1 << 1 00000002 2 00000000 00000000 00000000 00000010 | |
1 << 2 00000004 4 00000000 00000000 00000000 00000100 | |
1 << 3 00000008 8 00000000 00000000 00000000 00001000 | |
1 << 4 00000010 16 00000000 00000000 00000000 00010000 | |
1 << 5 00000020 32 00000000 00000000 00000000 00100000 | |
1 << 6 00000040 64 00000000 00000000 00000000 01000000 | |
1 << 7 00000080 128 00000000 00000000 00000000 10000000 | |
1 << 8 00000100 256 00000000 00000000 00000001 00000000 | |
1 << 9 00000200 512 00000000 00000000 00000010 00000000 | |
1 << 10 00000400 1024 00000000 00000000 00000100 00000000 | |
1 << 11 00000800 2048 00000000 00000000 00001000 00000000 | |
1 << 12 00001000 4096 00000000 00000000 00010000 00000000 | |
1 << 13 00002000 8192 00000000 00000000 00100000 00000000 | |
1 << 14 00004000 16384 00000000 00000000 01000000 00000000 | |
1 << 15 00008000 32768 00000000 00000000 10000000 00000000 | |
1 << 16 00010000 65536 00000000 00000001 00000000 00000000 | |
1 << 17 00020000 131072 00000000 00000010 00000000 00000000 | |
1 << 18 00040000 262144 00000000 00000100 00000000 00000000 | |
1 << 19 00080000 524288 00000000 00001000 00000000 00000000 | |
1 << 20 00100000 1048576 00000000 00010000 00000000 00000000 | |
1 << 21 00200000 2097152 00000000 00100000 00000000 00000000 | |
1 << 22 00400000 4194304 00000000 01000000 00000000 00000000 | |
1 << 23 00800000 8388608 00000000 10000000 00000000 00000000 | |
1 << 24 01000000 16777216 00000001 00000000 00000000 00000000 | |
1 << 25 02000000 33554432 00000010 00000000 00000000 00000000 | |
1 << 26 04000000 67108864 00000100 00000000 00000000 00000000 | |
1 << 27 08000000 134217728 00001000 00000000 00000000 00000000 | |
1 << 28 10000000 268435456 00010000 00000000 00000000 00000000 | |
1 << 29 20000000 536870912 00100000 00000000 00000000 00000000 | |
1 << 30 40000000 1073741824 01000000 00000000 00000000 00000000 | |
1 << 31 80000000 -2147483648 10000000 00000000 00000000 00000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment