如何解决有人可以告诉我如何设置或重置lua ..吗?
我想对数字中的特定位进行设置和重置。当我使用lua 5.1时,我无法使用API和移位运算符,因此它变得越来越复杂,请帮助我找到这个
解决方法
bit
库随固件一起提供。
阅读文档:https://nodemcu.readthedocs.io/en/release/modules/bit/
,如果知道要翻转的位的位置,则可以在没有外部库的情况下进行操作。
filename_202011120001.zip
filename_202011120002.tar.gz
filename_202011120003.rar
filename_202011120004.tar.bz2
#! /usr/bin/env lua
local hex = 0xFF
local maxPos = 7
local function toggle( num,pos )
if pos < 0 or pos > maxPos then print( 'pick a valid pos,0-' ..maxPos )
else
local bits = {} -- populate emtpy table
for i=1,maxPos do bits[i] = false end
for i = maxPos,pos +1,-1 do -- temporarily throw out the high bits
if num >= 2 ^i then
num = num -2 ^i
bits [i +1] = true
end
end
if num >= 2 ^pos then num = num -2 ^pos -- flip desired bit
else num = num +2 ^pos
end
for i = 1,#bits do -- add those high bits back in
if bits[i] then num = num +2 ^(i -1) end
end
end ; print( 'current value:',num )
return num
end
original value: 255
current value: 127
pick a valid pos,0-7
current value: 127
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。