![]() |
Forum Index : Microcontroller and PC projects : MMBasic: suggestion for enhancement to PEEK and POKE individual bits
Author | Message | ||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4311 |
Hi folks, Setting, clearing and querying individual bits is relatively expensive in MMBasic so I was wondering if the following (which I don't believe will take up additional token 'slots') might be worthwhile and more performant: ' Sets 'offset' bit in the memory allocated to 'var' to 'value' (0 or 1). POKE BIT var, offset, value ' Returns 'offset' bit from the memory allocated to 'var'. PEEK(BIT var, offset) Possibly for the POKE case it would be nicer to have POKE BITSET and POKE BITCLEAR instead of having the 'value' as a parameter ... and then we might have POKE BITFLIP too ![]() And on a related note we have a VAR command that could presumably be overloaded to provide a VAR SWAP for swapping two variables which is something I believe I have seen requests for on TBS ... though I know it isn't a great match to the purpose of the other VAR sub-commands. No worries if not, best wishes, Tom Edited 2021-08-04 20:25 by thwill MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10310 |
Can't see any speed advantage over existing methods. Poke would be slower as three variables to parse e.g. INC myvar%,1<<offset ? myvar% and (1<<offset) or if you need 0 or 1 ? choice(myvar% and (1<<offset) ,1,0) |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4311 |
Using INC is buggy if the bit is already set isn't it ? My current mechanism: ' Set bit i% of x% x% = x% Or 1 << i% ' Clear bit i% of x%. x% = (x% Or 1 << i%) Xor 1 << i% ' Get bit i% of x%. y% = (x% And 1 << i%) > 0 Best wishes, Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4311 |
In addition irrespective of performance I would argue this would be more readable: POKE BITSET var, offset POKE BITCLEAR var, offset y% = PEEK(BIT var, offset) And could also be written to work for var's other than 64-bit integers for those cases where a larger bit-set needs manipulating. YMMV, Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10310 |
clear bit as per standard C x%= x% and inv (1<<offset) |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4311 |
Thanks Peter, it had escaped my attention that we had a bitwise not, i.e. INV. Best wishes, Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |