Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:37 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

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 Kingdom
Posts: 4311
Posted: 10:22am 04 Aug 2021
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 10310
Posted: 10:50am 04 Aug 2021
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 4311
Posted: 11:12am 04 Aug 2021
Copy link to clipboard 
Print this post

  matherp said  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)


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 Kingdom
Posts: 4311
Posted: 11:22am 04 Aug 2021
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 10310
Posted: 11:30am 04 Aug 2021
Copy link to clipboard 
Print this post

clear bit as per standard C

x%= x% and inv (1<<offset)
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 11:51am 04 Aug 2021
Copy link to clipboard 
Print this post

  matherp said  clear bit as per standard C

x%= x% and inv (1<<offset)


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
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025