LoginLogin

AND vs &&/OR vs ||

Root / Programming Questions / [.]

VakoreCreated:
What's the difference? I know && vs &/|| vs | but not And/or Thanks

AND/OR/XOR compare each bit in a binary number separately && and || just check if each value is 0 or not 0, and are also optimized to not check the second value unless it's necessary If you want to do something like, check if X is between 0 and 10, you'd write that as
IF X>=0 && X<=10 THEN
This first checks X>=0, and if that's false, it skips checking X<=10.

I think I understand, and I remember learning about those somewhere, but just didnt understand how they worked In SB. So... how exactly would
if (button(0) and #A) && onGround > 0 then whatever
work? It checks if the button(0) function equals #A, or any bit from button(0) is #A?(forgot the number for that). What would the javascript equivalent be for that then? I'm going to start on an interpeter soon, and I figured that this would be an important hurdle to overcome to get something simple like Dropper or a raycaster running.

js uses & instead of AND

Ah, ok. Makes things easier for me. So | would be OR, and it looks like I have to simulate XOR by messing with tokens. Know any smilebasic programs that use XOR? Also, thanks for the help!

Logical XOR is !A != !B, since XOR is simply an logical unequality test. In SmileBASIC, bitwise XOR is used in almost all the "encryptor" programs here in SBS.

I think I understand, and I remember learning about those somewhere, but just didnt understand how they worked In SB. So... how exactly would
if (button(0) and #A) && onGround > 0 then whatever
work? It checks if the button(0) function equals #A, or any bit from button(0) is #A?(forgot the number for that). What would the javascript equivalent be for that then? I'm going to start on an interpeter soon, and I figured that this would be an important hurdle to overcome to get something simple like Dropper or a raycaster running.
If you want to check if only the A button bit is on you should do:
IF (BUTTON() AND #A)==#A THEN
  '...
ENDIF
By doing only the bitwise AND you are checking if the A button bit is set, therefore allowing for other buttons be pressed while A is pressed too.

Logical XOR is !A != !B, since XOR is simply an logical unequality test. In SmileBASIC, bitwise XOR is used in almost all the "encryptor" programs here in SBS.
Wouldn't
A != B
also work? Since ! is just setting the opposite of both?
I think I understand, and I remember learning about those somewhere, but just didnt understand how they worked In SB. So... how exactly would
if (button(0) and #A) && onGround > 0 then whatever
work? It checks if the button(0) function equals #A, or any bit from button(0) is #A?(forgot the number for that). What would the javascript equivalent be for that then? I'm going to start on an interpeter soon, and I figured that this would be an important hurdle to overcome to get something simple like Dropper or a raycaster running.
If you want to check if only the A button bit is on you should do:
IF (BUTTON() AND #A)==#A THEN
  '...
ENDIF
By doing only the bitwise AND you are checking if the A button bit is set, therefore allowing for other buttons be pressed while A is pressed too.
Ok, so that makes sure that the bit is only #A. I'm a little confused by how #A returns a number though. I understand it's a bit, but how does BUTTON() get a bit from a number?

#A is just 16 (binary 0000000010000) also (BUTTON() AND #A)==#A is the same as BUTTON() AND #A except it outputs 0 or 1 instead of 0 or 16 (or whatever #A is)

I understand numbers and binary. What I dont understand is how #A is stored and read by BUTTON(). Is it stored in binary and read as binary by BUTTON(), or is it just stored as 16 and BUTTON() converts it to binary?

I understand numbers and binary. What I dont understand is how #A is stored and read by BUTTON(). Is it stored in binary and read as binary by BUTTON(), or is it just stored as 16 and BUTTON() converts it to binary?
there is no thing as "converting to binary" as everything is implicitly stored in binary, so BUTTON does some bitwise stuff like: (pseudo-code)
if A is pressed:
  button := button | (1<<5)
since 1<<5 is the 5th bit on (16), the OR is kind of turning on the 5th bit of the button bitmask hope its easy to follow like subscribe hit the bell comment and see ya later *dies*

#A is just 16 (binary 0000000010000) also (BUTTON() AND #A)==#A is the same as BUTTON() AND #A except it outputs 0 or 1 instead of 0 or 16 (or whatever #A is)
yeah got my fields mixed this works though for things like pressing two buttons at the same time compare:
REPEAT UNTIL BUTTON() AND #L+#R
and
REPEAT UNTIL (BUTTON() AND #L+#R)==#L+#R

I understand numbers and binary. What I dont understand is how #A is stored and read by BUTTON(). Is it stored in binary and read as binary by BUTTON(), or is it just stored as 16 and BUTTON() converts it to binary?
there is no thing as "converting to binary" as everything is implicitly stored in binary, so BUTTON does some bitwise stuff like: (pseudo-code)
if A is pressed:
  button := button | (1<<5)
since 1<<5 is the 5th bit on (16), the OR is kind of turning on the 5th bit of the button bitmask hope its easy to follow like subscribe hit the bell comment and see ya later *dies*
to make this general, to check if a bit in a number is turned on in SB, you would do (NUM AND (1<<BIT))||0 which would return either 1 or 0 (lsb-0, which means the right-most bit is index 0)

you could also use (NUM>>BIT) AND 1 instead

I understand numbers and binary. What I dont understand is how #A is stored and read by BUTTON(). Is it stored in binary and read as binary by BUTTON(), or is it just stored as 16 and BUTTON() converts it to binary?
there is no thing as "converting to binary" as everything is implicitly stored in binary
Just to clarify a little more, numbers are always stored in binary. They're converted to decimal when you print them.

That's cool.