Log In  

Hi everyone,

I am a bit new to Pico-8, so I'm not sure if this is a known thing or not, or if it is even an issue.

Bitwise operations seem to act funny, specifically XOR.

For example this is how XORing something with 1 should work

    0000
xor 0001
=   0001

    0001
xor 0001
=   0000

I've tried implementing this in code by doing:

switch=0x00

if btnp(4) then
 bxor(switch,0x01)
 cls()
 print(switch)
end

But switch just stays at 0. I'm not sure if I'm doing something wrong or if this is just the way it's intended to operate.

Thanks for your time!

P#14989 2015-10-05 02:27 ( Edited 2015-10-10 03:07)

bxor, and other bitwise operations, returns a result of the operation, it doesn't change the variable itself, just like additions, substractions, ...
try:

switch=0x00

if btnp(4) then
switch=bxor(switch,0x01)
cls()
print(switch)
end

P#14990 2015-10-05 02:47 ( Edited 2015-10-05 06:47)

Right, my bad, I wrote the code above from memory and forgot to put in the switch variable before XOR.

Here is the code pasted from my program. XOR still doesn't seem to work unfortunately.

switch=0x02

cls()
print(switch)

while true do
    if btnp(4) then
        switch=bxor(switch,0x01)
        --switch+=1
        cls()
        print(switch)
    end
end
P#15005 2015-10-05 10:58 ( Edited 2015-10-05 14:58)

I thinks it's the structure of your program which is incorrect (i try it and it doesn't detect the input button), try with the _update and _draw functions :
switch=0x02

cls()
print(switch)

function _update()
if btnp(4) then
switch=bxor(switch,0x01)
cls()
print(switch)
end
end

function _draw()
end

or try a simpler test, like this one :

print(bxor(2, 1))

it work well for me

P#15006 2015-10-05 12:08 ( Edited 2015-10-05 16:08)

Ah yes, you're right, using the _update and _draw functions made it work. I was using a while true loop to quickly test the switch idea, but it must have interfered with the logic or output somehow.

The weird thing is that if you un-comment the switch+=1 line, it does print the incremented value.

Thanks for the help!

P#15007 2015-10-05 12:49 ( Edited 2015-10-05 16:49)

I think that the input functions (btn and btnp) don't work properly outside of the _update and _draw framework. Seems like that's what is confusing things here.

P#15014 2015-10-05 17:51 ( Edited 2015-10-05 21:51)

Ya, I think the issue is that using a while true loop means there is no fixed time step or something? Because when I use the while true loop and this code:

while true do
  if btnp(4) then
    switch+=1
    cls()
    print(switch)
  end
end

It works, but pressing the button for a moment makes it increment up to like 200 or more. Using _update it increments at a much more reasonable pace. So maybe using the bit switch and a while true loop just makes the switch so fast you never actually get to see it.

P#15211 2015-10-09 22:43 ( Edited 2015-10-10 02:43)

In theory the flags for btnp should be cleared after each time the _update callback is called (or would be called.) Maybe the timing is different, but if you check it in a tight loop like that I'm surprised it's only going up by 200. But I then again print functions are usually heavy-duty.

P#15212 2015-10-09 23:07 ( Edited 2015-10-10 03:07)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 17:42:53 | 0.008s | Q:17