Log In  


Hello everyone. Is there a way to write and read values to specific bits in a byte?
For example:
I have a variable 0x0000 .
I want to write a 1 to the 3rd bit so it'll be
0x0100 . How can i do that?
I want to later read that variable. How do I
check the value of the 3st bit? I haven't operated
with bits & bytes before.




1

Hi @collinthenewmaker:

Here is a quick program to demonstrate one easy way of reading bits. It makes use of the band() function.

Cart #showbits-2 | 2022-11-07 | Code ▽ | Embed ▽ | No License
1

To load that in Pico-8 immediate mode, type: load #showbits or click codeā–¼ just above.

Hope This Helps !


thanks guys, its very helpful


Note that the functions band and co are obsolete now (because of performance issue), you should use the operators.


Operators, @merwok ?

And what is CO ?


1

@dw817
I would guess "and company." As in band, bor and that whole collection of functions. They all have equivalent operators which (&, |, etc.) which are more efficient.


Hi @jasondelaat:

Ah, sorry, sometimes I take things very literally. :)
Co. Company. OK.

As for the & and | these are new to me. Let me ask, is BAND() in any danger of being removed ?

This to me:

        if i&2^j>0 then

Is not as easy for me to read as this:

        if band(i,2^j)>0 then

@dw817
You'd have to ask zep to know for sure but I suspect they're not going anywhere anytime soon.

The operators are a bit more efficient but the behaviour isn't exactly the same. The operators will throw an error if either argument is not a number. In the same situation the functions will return 0 which may be a useful safety net in some situations. So I'd guess you're fine.



[Please log in to post a comment]