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.

P#14888 2015-10-02 19:46 ( Edited 2015-10-09 20:39)

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

P#14905 2015-10-02 23:58 ( Edited 2015-10-03 03:58)

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.

P#14906 2015-10-03 02:18 ( Edited 2015-10-03 06:18)

"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?

P#14915 2015-10-03 10:45 ( Edited 2015-10-03 14:45)

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.

P#14928 2015-10-03 13:29 ( Edited 2015-10-03 17:29)

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

P#15171 2015-10-08 22:52 ( Edited 2015-10-09 02:52)

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
& 0b01111101
= 0b00001000 == 8

Binary OR (bor) makes a new number where it's 1 if either input is 1, otherwise 0
& 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
& 0b00001111
= 0b00000111 = 7
P#15176 2015-10-09 04:36 ( Edited 2015-10-09 08:36)

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

P#15190 2015-10-09 13:36 ( Edited 2015-10-09 17:36)

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

P#15199 2015-10-09 16:39 ( Edited 2015-10-09 20:39)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-17 01:57:09 | 0.008s | Q:21