LoginLogin

How to use AND operation

Root / FAQs / [.]

Timothyling55Created:
There is a statement in this tutorial that confused (https://smilebasicsource.com/page?pid=881&cid=14173). The statement goes as follows: IF B% AND 15 THEN '15 is the sum of all the direction buttons. AND returns the bits that are set in both values. Thus, if ANY of the direction bits are set, this conditional will be true and we'll erase the old player I am a beginner and I do not understand how does AND works, like, at all. So I just want to make sure that I understand how it actually works. Here's what I understand from it. 1. Each button and button combination has a different Identification Number 2. AND retrieves the integers on the left and right of it and turns it into binary 3. Hence, B% (Button being pressed) and 15 will turn into binary 4. For the D-pad, there are four bits (binary digits) designated to each direction, hence &b0000 when none of the buttons are pressed, and &b1111 when all of them are pressed (15 in deca) 5.15 in binary is &b1111 6. The AND function is like an "AND" logic gate 7. So say, the up button is pressed, in binary it will be &b0001. Compare that with 15, &b1111: &b0001 &b1111 where 0 = false and 1 = true. The AND function requires both bits in the same vertical row of the integers (B% and 15) to be true (1 & 1 = 1, true. 1 & 0 = 0, false.). So for the first row, 0 & 1 = 0, 2nd row, 0 & 1 = 0, 3rd row, 0 & 1 = 0 and the fourth row, 1 & 1 = 1. Put it all together: &b 0001. 8. The AND function will be true as long as any one of the bits turn out to be 1, and will be false if all of them are 0s. 9. IF statements work like: "IF (Condition = TRUE) THEN (Execution)" 10. Since we have at least one 1 in our AND statement, that means it's TRUE, and the condition of the IF statement is fulfilled, executing the statement. 11. Hence if any buttons on the D-pad are pressed, it will execute the statement. Please correct me if I'm wrong. I'm a beginner at BASIC, and it was very difficult to find an explanation to this online, not that I found any. My sister who is more experienced than me at programming and electronics explained it like this. She has never used BASIC tho.

Basically, except AND returns the full binary number, not just true or false. &b0001 AND &b1111 = &b0001, &b1101 AND &b1110 = &b1100, etc. IF is actually IF (Condition != 0) THEN .... So in this case, unless all the bits are 0, the code will run.

The
DensetsuDensetsu is Japanese for Legend
says you are an idiot
can anyone even read this?
I wish I could help but I can't explain it any better than auceps. In fact I can't even explain it as well as auceps.

Ok lets start at the start, what we are talking about here is called Boolean Logic. I think this link might be a little more helpful than the wikipedia one (https://computer.howstuffworks.com/boolean.htm). This is not unique to BASIC. Computers are basically built on Boolean logic. Everything in your computer is represented by bits. A bit is just jargon for Binary Digit. In computers it is 0 if the voltage is low and 1 if the voltage is high. So, when you call the button command, instead of having you specify which button you want to check, it instead gives you ALL of the the buttons. The buttons are encoded where each bit of the number returned represents one of the buttons. When you AND it with a constant like say #A, it is masking off all of the bits except the one for the A button. Then you can check if the result is zero or non-zero to see if the button is held down or not. The only real difference when masking with 15 (1111 in binary) is that you are checking for at least one of four bits being set. If you print out all of the constants, you may notice that they are powers of 2. 2^0 = 1, 2^1 = 2, 2^2 = 4, etc. Anyway, give the link a read and see if it helps or not. You may have a lot more questions afterward, and that is expected and good.