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.

P#120268 2022-11-07 17:55 ( Edited 2022-11-07 18:00)

P#120273 2022-11-07 18:45
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 !

P#120274 2022-11-07 18:50 ( Edited 2022-11-07 18:53)

thanks guys, its very helpful

P#120279 2022-11-07 20:24

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

P#120282 2022-11-07 22:02

Operators, @merwok ?

And what is CO ?

P#120289 2022-11-07 23:10 ( Edited 2022-11-08 00:31)
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.

P#120292 2022-11-08 00:27

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
P#120293 2022-11-08 00:32

@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.

P#120306 2022-11-08 11:09

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 18:53:21 | 0.074s | Q:26