"This AND That OR Whatever" (from Vol. 2, No. 1) Those of you who read this "Down to BASIC" column closelymay have been baffled by the strange usage of the AND and ORcommands in the previous two issues. As a matter of fact, Imeant to explain those unusual applications at that time;however, there was so much to talk about PEEKs and POKEsthat the information on AND and OR just got squeezed out. But that was last month -- now we have an entire column toexplain things. You probably thought that AND and OR werealways found in IF statements. Of course, that is the waythe authors of BASIC intended for us to use them. Remember,when AND separates conditions in an IF statement, each andevery one of the individual conditions must be true for theentire statement to be considered true. On the other hand,when OR separates conditions in an IF statement, the entirestatement is considered to be true even if only oneindividual condition is true. The use of AND and OR in IF statements is not particularlycryptic -- it does not take a programming genius to figureout what is going to happen when the computer encounters astatement like this: IF (2+2=4) AND (4+4=8) THEN PRINT "BOTHTRUE". But who among my thousands of readers knows whatwill happen when the computer runs across a statement thatreads LET X = 125 AND 95? This is the kind of applicationthat may have confused you in previous months' issues. AND and OR are actually mathematical operations -- just asaddition, subtraction, multiplication, and division areoperations. As an operation, each requires two operands (ortwo numbers that are used in the calculation) and eachproduces one result. As confusing as it may look, there aresome very simple rules that make AND and OR operations onnumbers quite logical (no pun intended). To understand theoperations, pretend for a moment that there are only twonumbers in the whole world -- zero and one. With only twonumbers, you can be assured that the result of any AND andOR operation will be one of those numbers -- either zero orone. And now for the rules. Others have tried to explain them bythinking of zero and one as "true and false", or as "on andoff", or as "yes and no" -- but I don't go for that. Let'sjust accept them as rules, just as you accepted themultiplication tables when you were in elementary school.When you AND two operands together, the result is one onlyif both operands are one; otherwise, the result is zero.When you OR two numbers together, the result is zero only ifboth operands are zero; otherwise, the result is one. Of course, the real world contains more numbers than justzero and one. But, as we learned last month, any integernumber between zero and 255 can be expressed in binaryformat ABCDEFGH, where each letter A through H represents azero or a one according to the formula: Number = (A x 128) + (B x 64) + (C x 32) + (D x 16) + (E x 8) + (F x 4) + (G x 2) + H In computer-ese language, each of those numerals A through Hare referred to as "bits". As you can see, there are eight"bits" in the representation of each integer (which can bestored in one byte of the computer's memory). For example,the number one hundred can be expressed in binary format as01100100. See for yourself by using the formula above. How does this confusing binary format stuff relate to theAND and OR operations? Like this: whenever BASIC sees anAND or OR operation between two integers, it converts eachof the two operands into binary format. It then creates anew number (in binary format) which is constructed by doingan AND or OR operation on each of the eight corresponding"bits". That may not make too much sense in words, so let's take anexample to illustrate. To find the result of 125 AND 95, wemust first convert each into binary format -- 01111101 and01011111, right? If we perform an AND operation on thefirst bit in each number, we find 0 AND 0 equals 0. So thefirst bit of the answer is 0. Using the second bits, wefind 1 AND 1, which produces 1. The third bits yield 1 AND0, for a value of 0. Continuing through to the eighth bit,the final result is 01011101, which is equal to 93. InBASIC, type PRINT 125 AND 95, and the answer should be 93.Amazing! As an exercise, try 125 OR 95 ... your answershould be 127. The program found in the listing at the end of this articlehas a very limited purpose -- it accepts two numbers fromthe user. It converts each number into binary format anddisplays them on the screen. It then performs an AND and anOR operation on the pair of numbers, and displays the answerin both binary and decimal format. Type the program in andtry it. The binary numbers are displayed in a boxedscoreboard-type format so that you can see and visualize thebit-by-bit operations more easily. Notice that both AND andOR are commutative operations -- that is, it does not matterin which order the operands appear. And now that you understand AND and OR operations, what goodare they? Well, here is one for the trivia books: Did youknow that the result of X AND 1 will always be zero when Xis an even number, and one for an odd number? Do you knowwhy? There are some other obscure functions of AND and OR, butone of the most useful tricks involving them is "bit-mappedvariables", which is just a fancy way of saying "packing alarge amount of information into one number". The trickhere is in using each of the eight bits in the binary formof the number to serve as a flag or an indicator of sometrue-or-false or yes-or-no type condition. As a result, aclever programmer can use one number to hold as many aseight true-or-false flags. Once again, an illustration may make things clearer. InBASIC, type DEF SEG=0. Each of the eight bits in the binaryform of the number stored in location 1047 represents thecondition of one of eight special keys. Each of the bits(from A through H in our terminology) corresponds to theINSERT, CAPS, NUM LOCK, BREAK, ALT, CTRL, left SHIFT, andright SHIFT keys, respectively. A bit-value of oneindicates that the key is "on" or is presently beingpressed; a bit-value of zero indicates that the key is"off". You can check on the status of any of those eight keys withthe proper AND statement. PEEK(1047) AND 128 will equal 128if the INSERT key is pressed, zero if not. You cansubstitute 64 instead of 128 in the expression above andcheck on the CAPS key. Similary, the values 32, 16, 8, 4,2, and 1 can be used to check on the NUM LOCK, BREAK, ALT,CTRL, left and right SHIFT keys. Use this month's programto visualize the effect of each AND operation. The moreadventurous among you may even experiment with OR instead ofAND -- in that manner, you can change the bits to suityourself. For example: POKE 1047, PEEK(1047) OR 64 will setthe second bit to one, and will have the same effect aspushing the CAPS key. You can use bit mapped variables in your own programming.Suppose you were writing a program to store some descriptivedata on each of the members of your computer club. You handout a questionnaire to your members, and it consists ofeight yes-or-no questions -- are you male? over 6 feet tall?over 200 pounds? have blonde hair? have blue eyes? wearglasses? over 30 years old? married? Do you see how youcan store all of that information in one number? There willbe one number for each member, and each of the eight bits inthe binary form of that number will be equal to one if theanswer to each of the questions is yes, or zero if theanswer is no. You can use AND and OR to change the bitvalues or to check on their status. Admittedly, AND and OR operations on bit mapped variablesare rather advanced techniques, and ones that you may neverreally feel comfortable with. Nevertheless, I hope that youunderstand a little bit more about them now, and when yousee them in a program listing, will not think they aretypographical errors. I look forward to hearing from youvia mail, care of ONE THOUSAND magazine. See you nextmonth.