環境
系統:Windows 10
引擎:Lua5.3.5
用途
例如:以標誌位記錄一系列任務的完成情況,通過其差值位移次數可以判斷這是第幾個任務(即:任務索引)。
實例
local old_flag = 223 -- 11011111
local new_flag = 255 -- 11111111
local diff_flag = new_flag - old_flag -- 32
local temp_flag = diff_flag
local index = 0
print('flag change:')
while temp_flag > 0 do
temp_flag = temp_flag >> 1
print(temp_flag)
index = index + 1
end
print('index:', index)
結果
flag change:
16
8
4
2
1
0
index: 6
[Finished in 0.1s]
以上簡單回顧。
參考資料:
Lua 5.3 參考手冊 https://www.runoob.com/manual/lua53doc/manual.html