| Home CBM ASCII-X BASIC Disk Commands Enter RUN mode Program Format Secret Variables Variable Format Expressions Keywords (divide) (equal) (less) (minus) (more) (multiply) (plus) (power) Abs And Append Asc Atn Auto Backup Bank Begin Bend Bload Boot Box Bsave Bump Catalog Char Chr Circle Close Clr Cmd Collect Collision Color Concat Cont Copy Cos Data Dclear Dclose Dec Def Delete Dim Directory Dispose Dload Do Dopen Draw Ds Ds string Dsave Dverify El Else End Envelope Er Err Exit Exp Fast Fetch Filter Fn For Fre Get Get num Getkey Go Gosub Goto Graphic Gshape Header Help Hex If Input Input num Instr Int Joy Key Left Len Let List Load Locate Log Loop Mid Monitor Movspr New Next Not Off On Open Or Paint Peek Pen Pi Play Pointer Poke Pos Pot Print num Pudef Quit Rclr Rdot Read Record Rem Rename Renumber Restore Resume Return Rgr Right Rlum Rnd Rreg Rspcolor Rsppos Rsprite Run Rwindow Save Scale Scnclr Scratch Sgn Sin Sleep Slow Sound Spc Sprcolor Sprdef Sprite Sprsav Sqr Sshape St Stash Step Stop Str Swap Sys Tab Tan Tempo Then Ti Ti string To Trap Troff Tron Until Using Usr Val Verify Vol Wait While Width Window Xor Syntax Tokens C128 D64plus Disk Escape Codes Hardware PCxface PETSCII Pet2asc Futurama IBM PC-AT Contact Games Glossary Hall of fame Hall of shame Miscellaneous Privacy policy Programming Twisty puzzles |
subject AND mask
Evaluate a bit-wise boolean operation.
The AND operator is often used in logical expressions, and may be considered a logical operator. However, this is not really the case and can have nasty consequences if you're not aware of how it actually functions. First, if the either parameter is a string, a TYPE MISMATCH ERROR is generated. Next, each parameter, if it is floating-point, is then converted to an integer; if the result is not a legal value (see above) then ILLEGAL QUANTITY ERROR is generated. Finally, each of the 16 corresponding bit pairs is evaluated and the corresponding bit in the result is assigned based on this truth table:
For bit-wise operations, it may be helpful to think of the mask as filtering the subject, such that if a mask bit is 1, the result bit is the same as the original subject. If the mask bit is 0, the result bit will always be zero. So you might also think of it as turning bits off. I hope this doesn't confuse you, but another way to look at it is a simple 1-bit multiply. This is different than a normal multiply because each bit pair is processed individually (instead of all bits being considered a single number). If both of the parameters are either 0 or -1 then the result will also be either -1 or 0, which is completely logical. BASIC operators which return a boolean result always return -1 for true or 0 for false so this works well most of the time. However, using AND with other non-zero values (besides -1) will typically produce non-logical values if the resullt is not zero. This is why AND is more properly called a bit-wise boolean operation (instead of a logical operator). Examples are provided below, but to really understand them, you need to know how to convert a decimal number into binary (and back again). I won't try to explain that here; you may find this web page helpful for experimenting, or use a calculator. The AND operator has a very low operator priority (6); only the OR operator has a lower priority. Of course the user may invoke parentheses to change the order of evaluation. Some "logical" examples:
Some examples about operator priority:
Some examples demonstrating bit-wise complement:
© H2Obsession, 2014 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||