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!



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


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

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


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!


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.


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.


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.



[Please log in to post a comment]