---- START NEW MESSAGE --- Received: from cherry.ease.lsoft.com [209.119.0.109] by dpmail10.doteasy.com with ESMTP (SMTPD32-8.05) id ADAE3A2F006C; Sun, 01 Feb 2004 12:12:30 -0800 Received: from PEAR.EASE.LSOFT.COM (209.119.0.19) by cherry.ease.lsoft.com (LSMTP for Digital Unix v1.1b) with SMTP id <2.00CC8016@cherry.ease.lsoft.com>; 1 Feb 2004 15:12:24 -0500 Received: from MITVMA.MIT.EDU by MITVMA.MIT.EDU (LISTSERV-TCP/IP release 1.8e) with spool id 2688 for PICLIST@MITVMA.MIT.EDU; Sun, 1 Feb 2004 15:12:18 -0500 Received: from MITVMA (NJE origin SMTP@MITVMA) by MITVMA.MIT.EDU (LMail V1.2d/1.8d) with BSMTP id 3294; Sun, 1 Feb 2004 15:10:29 -0500 Received: from embedinc.com [68.162.221.141] by mitvma.mit.edu (IBM VM SMTP Level 430) via TCP with ESMTP ; Sun, 01 Feb 2004 15:10:28 EST X-Comment: mitvma.mit.edu: Mail was sent by embedinc.com Received: from 192.168.0.3 (main) by mail.embedinc.com ; 2004 FEB 01 15:11:28 EST References: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1106 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Message-ID: <001501c3e8ff$68b87750$0300a8c0@main> Date: Sun, 1 Feb 2004 15:10:15 -0500 Reply-To: pic microcontroller discussion list Sender: pic microcontroller discussion list From: Olin Lathrop Subject: Re: [PIC:] AND, and OR To: PICLIST@MITVMA.MIT.EDU Precedence: list X-RCPT-TO: Status: U X-UIDL: 371856770 Ruben Undheim wrote: > Since I am relatively new to PIC-programming, and since I am not an > expert in mathematics (I'm still quite young!), > I wondered what really happens when you AND's to numbers. > Why is for example "50 AND 100 = 32" (decimal numbers) or > "0xAF AND 0xCC = 0x8C" ???? > - and what happens when you OR's to numbers? > > Without understanding this, I don't see what use I can get from the > ANDLW, ANDWF, IORWF, XORWF, IORLW, XORLW-commands. The best way to define these operations is with a table. Since they operate on two input bits, there are only 4 possible different input conditions. The single output bit can take on two different states for each of the 4 possible input conditions. 2**4 = 16, so there are only 16 possible different logic operations you can perform on two input bits to make one output bit. Some of these come up a lot or are generally useful and have been given names. Here is a table of all the possibilities: ---------- Outputs for ---------- case In 1,1 In 1,0 In 0,1 In 0,0 Name ---- ------ ------ ------ ------ --------------------------- 1 0 0 0 0 always zero, input ignored 2 0 0 0 1 NOR = not OR 3 0 0 1 0 (not A) AND B 4 0 0 1 1 not A 5 0 1 0 0 A AND (not B) 6 0 1 0 1 not B 7 0 1 1 0 XOR 8 0 1 1 1 NAND = not AND 9 1 0 0 0 AND 10 1 0 0 1 not XOR 11 1 0 1 0 B 12 1 0 1 1 (not A) OR B 13 1 1 0 0 A 14 1 1 0 1 A OR (not B) 15 1 1 1 0 OR 16 1 1 1 1 always 1, input ignored Note that some of these cases aren't really a function of both inputs. Case 1 is a wire to ground, case 16 is a wire to Vdd, case 11 is just the B input and case 13 is just the A input. Cases 4 and 6 require an inversion operation, but are still only a function of one input (the other input is irrelevant and doesn't enter into the equation). Given that, the following 10 logic operations on two inputs to make one output remain: ---------- Outputs for ---------- case In 1,1 In 1,0 In 0,1 In 0,0 Name ---- ------ ------ ------ ------ --------------------------- 2 0 0 0 1 NOR = not OR 3 0 0 1 0 (not A) AND B 5 0 1 0 0 A AND (not B) 7 0 1 1 0 XOR 8 0 1 1 1 NAND = not AND 9 1 0 0 0 AND 10 1 0 0 1 not XOR 12 1 0 1 1 (not A) OR B 14 1 1 0 1 A OR (not B) 15 1 1 1 0 OR Note that these can all be expressed in terms of the 3 basic operations AND, OR, and XOR, with inversion added as needed. AND is 1 iff both inputs are 1. OR is 1 iff either input is 1. XOR is 1 iff the two inputs differ. AND, OR, and XOR only make sense at the bit level. These are inherently operations that work on two input bits to produce an output bit. The byte wide operations for these in a PIC do 8 independent bit operations at once. The two input bits 0 produce the output bit 0, the input bits 1 produce the output bit 1, etc. For example: First byte value: 00001111 Second byte value: 10101010 AND: 00001010 OR: 10101111 XOR: 10100101 There are many useful ways to employ these operations, so I'll just point out a few. AND can be used to mask in just a particular bit field and set the remaining bits to zero. ANDing with 00000111 is "masking in" the low 3 bits of the original value. XOR can be thought of as a selective invert. XORing a byte with 11111111 is the same as inverting it. XORing with 00001111 is inverting the low nibble only. XOR can also be used as a compare. The result is zero if and only if both input bytes were equal. XOR can also the thought of "make the bit mask for the bits that are different". This can be useful, for example, to determine which bits changed from last time on a port B interrupt on change. There's more, but that's enough for one PIClist message. ***************************************************************** Embed Inc, embedded system specialists in Littleton Massachusetts (978) 742-9014, http://www.embedinc.com -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads .