Achoriham Posted January 15, 2011 Report Posted January 15, 2011 Hi, I need a little help with LUA (once again). I do perfectly understand how to read and write bytes, words, etc in LUA, but I need to change single bits now. Tried to find any info on that in the forum, but didn't find anything. So, how can I read and write values of bits in LUA? please help Potroh
Pete Dowson Posted January 17, 2011 Report Posted January 17, 2011 So, how can I read and write values of bits in LUA? At present you have to read the byte or word or whatever, then use the Logic library facilities or And, Or, Xor to clear, set or toggle the bit(s), and write the changed value back. So, for example: x = ipc.readUB(0x1234) -- read byte to change x = logic.And(x, 254) -- clear bit 0 (worth 1, 255-1 = 254) ipc.writeUB(0x1234, x) or, the same more efficiently (and safer, avoiding possible other changes to the same offset by others between the read and write): ipc.writeUB(0x1234, logic.And( ipc.readUB(0x1234), 254)) These sorts of operations can become quite frequent, though, and I am considering adding "SetBits", "ClearBits" and "ToggleBits" functions to the ipc library. Maybe soon ... Regards Pete
Achoriham Posted January 17, 2011 Author Report Posted January 17, 2011 These sorts of operations can become quite frequent, though, and I am considering adding "SetBits", "ClearBits" and "ToggleBits" functions to the ipc library. Maybe soon ... Hi Pete, Many thanks for the info (the lesson rather). Starts to be clearer now, but 'Setbits, etc. function would truly solve a lot of problems for me - and hopefully others too. Please think of it when got the time... best regards Potroh
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now