How To

Check and set bit in memory?

0

S3B4 2020-04-28 00:33

I’ve been experimenting with memory rom dark magic and want to know if there’s a way to check if a specific bit in memory is turned on or off, and depending on its state, turn it on/off


was8bit 2020-04-28 01:43

PEEK POKE reads or writes multiple bits at a time... it takes binary math to ensure only one bit is read or written....


was8bit 2020-04-28 01:53

Although it is an advanced program, i do it here...

https://lowresnx.inutilis.com/topic.php?id=854


nathanielbabiak 2020-04-28 19:34 (Edited)

There's 8 bits in a byte, numbered 0 to 7. The lowest bit (0) represents the number 1. The next lowest (1) represents the number 2. Here's where it gets confusing... The next lowest bit (2) represents the number 4, and the one after that (3) represents the number 8. So, you actually have to double the number each time. Ultimately, the highest bit (7) of a byte represents number 128.

So, with this you can check any individual bit with AND. For example:

IF X AND 16 THEN
'DO SOMETHING IF BIT (4) IS SET
ELSE IF X AND 64 THEN
'DO SOMETHING IF BIT (6) IS SET
END IF

FYI, the formula to double each time is 2 ^ BIT, where BIT is numbered 0 to 7.


Timo 2020-05-01 10:45

Real CPUs don't have access to single bits, they always handle at least bytes. So any bit manipulation is always: PEEK byte, manipulate value, POKE byte.


Log in to reply.