AND vs &&/OR vs ||
Root / Programming Questions / [.]
VakoreCreated:
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 THENThis 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 whateverwork? 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.
I think I understand, and I remember learning about those somewhere, but just didnt understand how they worked In SB. So... how exactly wouldIf you want to check if only the A button bit is on you should do:if (button(0) and #A) && onGround > 0 then whateverwork? 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 (BUTTON() AND #A)==#A THEN '... ENDIFBy 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 != Balso work? Since ! is just setting the opposite of both?
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?I think I understand, and I remember learning about those somewhere, but just didnt understand how they worked In SB. So... how exactly wouldIf you want to check if only the A button bit is on you should do:if (button(0) and #A) && onGround > 0 then whateverwork? 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 (BUTTON() AND #A)==#A THEN '... ENDIFBy 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.
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+#Rand
REPEAT UNTIL (BUTTON() AND #L+#R)==#L+#R
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)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*
Just to clarify a little more, numbers are always stored in binary. They're converted to decimal when you print them.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