BBC BASIC for Windows
Programming >> BBC BASIC language >> IF (....) STATEMENTS
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1390800591

IF (....) STATEMENTS
Post by chrispc on Jan 27th, 2014, 04:29am

Richard; thanks for your Sudoku help. I am still looking into the bitmap stuff. However the code you ended with:
IF (num% AND (num%-1)) = 0 THEN just_one_bit_set = TRUE
has me confused. I tried the following uses of that form and got these answers:
1.
A%=0
B%=0
C%=1
D%=2

IF (D% AND C%)=0 THEN
PRINT "YES"
ELSE PRINT "NO"
ENDIF

2.
IF(4 AND 7)<5 THEN
PRINT "YES"
ELSE PRINT "NO"
ENDIF


Then I tried
PRINT (4 AND 7)
and this gave the answer 4, so I thought maybe it was considering only the first-mentioned number. But
3.
IF (7 AND 4)<5 THEN
PRINT "YES"
ELSE PRINT "NO"
ENDIF
also gave the answer "YES".

All the above programs gave the answer "YES". Perhaps you dealt with this in your Tutorial notes but I couldn't find it.
Can you explain please?
Thanks.
chrispc





Re: IF (....) STATEMENTS
Post by ScriptBasic on Jan 27th, 2014, 08:20am

Quote:
All the above programs gave the answer "YES".


Your examples (1-3) are printing the correct results.


Re: IF (....) STATEMENTS
Post by admin on Jan 27th, 2014, 08:30am

on Jan 27th, 2014, 04:29am, chrispc wrote:
Can you explain please?

There is a truth-table for the AND operator in the documentation here:

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin4.html#and

If you apply that table to the binary values of your numbers everything should become clear. For example in the case of 4 AND 7:

4= 1 0 0
7= 1 1 1
4 AND 7= 1 0 0

So you can see that 4 AND 7 is 4, from which the other results follow.

If you are not familiar with Boolean algebra I would recommend that you spend some time learning about it, since it is so fundamental to programming.

Richard.

Re: IF (....) STATEMENTS
Post by ScriptBasic on Jan 27th, 2014, 6:00pm

Based on Chris's response, I get the feeling he thought using the AND would build a macro of values to compare against.

IF(4 AND 7)<5 THEN

What I think Chris expected was the results from this.

IF (4<5) AND (7<5) THEN

Which would print NO for the example.