专业编程基础技术教程

网站首页 > 基础教程 正文

lua位运算 配置文件通道开关 lua位运算效率低

ccvgpt 2024-10-23 09:30:31 基础教程 9 ℃

--

[[

lua位运算 配置文件通道开关 lua位运算效率低

Lua提供了bit库,可以对变量数据进行位运算,在某些应有场景,我们得确需要在lua中对数据进行位移,或是进行“与,或,非”,进制转换等操作。

]]--

----位运算操作

record_bit = 1

---如下配置就比较容易看懂每个单元的使能情况,

rec_ch =

{

ch_1 = 1,

ch_2 = 0,

ch_3 = 0,

ch_4 = 1

}

function enable_ch()

ch_nums = #(rec_ch)

print("rec_ch nums "..ch_nums)

record_bit = rec_ch["ch_1"] | (rec_ch["ch_2"]<<1) | (rec_ch["ch_3"]<<2) | (rec_ch["ch_4"]<<3)


-- record_bit = ch_1 |(ch_2 <<1) -- | (rec_ch[ch_3] <<2) | (rec_ch[ch_4] <<3)

print("record bit "..record_bit)

end

enable_ch() --这里只是运算看一下结果

--此脚本可以清晰的去控制每个位的开关使能情况,如果使用一个0x09就非常的不直观。

Tags:

最近发表
标签列表