28 lines
851 B
Text
28 lines
851 B
Text
local function set_right_bits(bits, e, n)
|
|
if e == 0 or n <= 0 then return bits end
|
|
local bits2 = bits:clone()
|
|
for i = 1, e - 1 do
|
|
local c = bits[i]
|
|
if c == 1 then
|
|
local j = i + 1
|
|
while j <= i + n and j <= e do
|
|
bits2[j] = 1
|
|
j += 1
|
|
end
|
|
end
|
|
end
|
|
return bits2
|
|
end
|
|
|
|
local b = "010000000000100000000010000000010000000100000010000010000100010010"
|
|
local tests = {{"1000", 2}, {"0100", 2}, {"0010", 2}, {"0000", 2}, {b, 0}, {b, 1}, {b, 2}, {b, 3}}
|
|
for tests as test do
|
|
local bits = test[1]
|
|
local e = #bits
|
|
local n = test[2]
|
|
print($"n = {n}; Width e = {e}:")
|
|
print($" Input b: {bits}")
|
|
bits = bits:split(""):map(|c| -> c:byte() - 48)
|
|
bits = set_right_bits(bits, e, n)
|
|
print($" Result: {bits:concat("")}\n")
|
|
end
|