-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex13.6.lua
45 lines (40 loc) · 1.1 KB
/
ex13.6.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- local function debug_print_string(s)
-- for i = 1, #s do
-- print(string.format("%X", string.unpack("b", s, i)))
-- end
-- end
local function NewBitArray(n)
local length = (n + 7) // 8
return string.rep("\0", length)
end
local function SetBit(a, n, v)
local index = (n + 7) // 8
local bit = n - (index - 1) * 8
local byte = string.unpack("b", a, index)
if v then
byte = byte | (1 << (bit - 1))
else
byte = byte & ~(1 << (bit - 1))
end
byte = string.pack("b", byte)
local prefix = string.sub(a, 1, index - 1)
local suffix = string.sub(a, index + 1)
return string.format("%s%s%s", prefix, byte, suffix)
end
local function TestBit(a, n)
local index = (n + 7) // 8
local byte = string.unpack("b", a, index)
local bit = n - (index - 1) * 8
byte = byte & (1 << (bit - 1))
return byte ~= 0
end
local a = NewBitArray(1)
assert(not TestBit(a, 1))
a = SetBit(a, 1, true)
assert(TestBit(a, 1))
local b = NewBitArray(16)
assert(not TestBit(b, 1))
b = SetBit(b, 9, true)
assert(TestBit(b, 9))
b = SetBit(b, 9, false)
assert(not TestBit(b, 9))