? AND vs &&/OR vs || (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password

AND vs &&/OR vs ||

  • #1 ✎ 157 Vakore What's the difference? I know && vs &/|| vs | but not And/or Thanks Posted
  • #2 ✎ 1848 12Me21 Power User Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself 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. Posted
  • #3 ✎ 157 Vakore 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. Posted
  • #4 ✎ 1848 12Me21 Power User Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself js uses & instead of AND Posted
  • #5 ✎ 157 Vakore 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! Posted
  • #6 ✎ 370 hakke RNG Hidden Easter Eggs Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength 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. Posted
  • #7 ✎ 370 hakke RNG Hidden Easter Eggs Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength
    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.
    Posted
  • #8 ✎ 157 Vakore
    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?
    Posted
  • #9 ✎ 1848 12Me21 Power User Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself #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) Posted
  • #10 ✎ 157 Vakore 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? Posted
  • #11 ✎ 370 hakke RNG Hidden Easter Eggs Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength
    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*
    Posted
  • #12 ✎ 370 hakke RNG Hidden Easter Eggs Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength
    #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
    Posted
  • #13 ✎ 370 hakke RNG Hidden Easter Eggs Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength
    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)
    Posted
  • #14 ✎ 1848 12Me21 Power User Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself you could also use (NUM>>BIT) AND 1 instead Posted
  • #15 ✎ 215 niconii Video Games I like to play video games! Hobbies Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Drawing I like to draw! Hobbies
    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.
    Posted
  • #16 ✎ 1282 rando Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Second Year My account is over 2 years old Website Forum Leader Hidden Achievements That's cool. Posted