Log In  


E.g. for storing text (hiscore name, anyone?). Since each number is 4 bytes long, to make standard "Top10" list you'd use only 40 bytes (results only). Which leaves 216 bytes free, that could be used for names, each 21 letters long (well, 21.6 actually, but we can't have 0.6 of character, now can we?). I know we can peek/poke into save data (although it seems memory map haven't been updated in manual), but I'm not sure if it would save.

1


You can also use the existing functions to just save/load it four bytes at a time.


Peek/Poke for persistent data seems a bit bugged to me right now.
I expected peek(0x5f80) and pget(0) to both get the same thing after a pset(0,1), but the former returns 3 and the latter returns 1.
Either that or, as darkhog just said, the memory layout needs to be updated.


"You can also use the existing functions to just save/load it four bytes at a time."

Is it bad moment to say that I don't understand bitwise operations, like, at all?


They're easier to understand if you think of them in terms of strings of ones and zeros. Identity.

Being serious, now, bitwise operations are simply taking the binary strings of numbers and doing logic on the bits.
I'd google it, bitfields are super important programming concepts.


Let me put it the other way: I've tried to learn them, but all explanations I've found didn't get to me.


binary operations are lots of fun and definitely worth learning!

let's take two numbers 10 and 125 and do some stuff with them:

_10 == 0x0a == 0b00001010
125 == 0x7d == 0b01111101

once you've got two binary numbers you can do binary operations with them:

Binary AND (band) makes a new number where it's 1 where both inputs are 1 otherwise 0
  0b00001010
& 0b01111101
= 0b00001000 == 8

Binary OR (bor) makes a new number where it's 1 if either input is 1, otherwise 0
  0b00001010
& 0b01111101
= 0b01111111 == 127

Binary Right Shift (shr) makes a new number by shifting all the bits right a certain number of bits

0b01111101 >> 4 (move everything right 4 bits)
0b00000111 == 7

Binary Left Shift (shl) makes a new number by shifting all the bits left a certain number of bits

0b01111101 << 4 (move everything left 4 bits)
0b11010000 == 208 (this is assuming limited to 8 bits since it gets clipped, pico8 uses 16 bit numbers though)

So you can for example store 2 4 bit numbers in a single 8 bit number and then extract them again.

0b00001010 ==10
0b00000111 == 7

shift one number << 4 bits
0b1010 0000 == 160
0b0000 0111 == 7
and OR them together
0b1010 0111 == 167 

you can then store the byte with poke

to split them apart again into two 4 bit numbers
to get the high nybble
  0b10100111 == 167
& 0b11110000
= 0b10100000
then shr 4
= 0b00001010 = 10

to get the low nybble
  0b10100111
& 0b00001111
= 0b00000111 = 7

Thanks, that got to me. However it's kinda sad that Pico doesn't allow to write binary numbers directly into the editor (like binary string ending with b, example 11011b).


Most languages support something like the 0bnnnnnnnn notation used in impbox's post.

If you just need to address a single bit, youc an always do something like:

shl(1, n)

where n is the bit you want to address. Note, it starts at zero and count from the right:

  v---------------------- bit 7
0b11110000
         ^------- bit 0

If you want to construct a mask, you can add them all up. If you want to do it in your head, just add up all the positions with a 1 in them, counting by powers of two (1,2,4,8,16,32,...)

So 0b11011:

    v---- this position has value 4, but it's not counted because the bit is zero
0b11011
  ^^ ^^-------- 1
  || |--------- 2
  ||----------- 8
  |----------- 16 +
---------------------
               27 =

In time you will start to see that as 31 ( = 0b11111) minus 4 ( = 0b100) = 27.

You can always punch it into a calculator in binary mode, then switch to hex or decimal mode to get the number in one of the bases you can enter directly.

Hope that helps.



[Please log in to post a comment]