AND
When we use logical bitwise AND we are making a bit-to-bit comparison of two 8-bit values. Each bit is compared within the same position and if both match 1 then 1 is the result. See logic table below for further illustration.
This operation is particularly useful for masking bits. Certain bits are preserved or cleared based on the mask used.
Note, it is possible to include A as the first parameter but this can be ommitted due to it only ever supporting A. i.e. AND B and AND A, B are the same.
| A | ? | Result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| Example | ||
| A | B | Result |
| 10010011 | 10101110 | 10000010 |
We can think of the logic of AND quite easily in the perspectives of other programming languages. Take the following psuode code for example:
- if( false AND false ) this will never fire
- if( false AND true ) this will never fire
- if( true AND false ) this will never fire
- if( true AND true ) then do this
| Instruction | Description | Opcode |
|---|---|---|
| AND A | Compare A with itself | A7 |
| AND B | Compare the 8-bit number in B register with value in A register | A0 |
| AND C | Compare the 8-bit number in C register with value in A register | A1 |
| AND D | Compare the 8-bit number in D register with value in A register | A2 |
| AND E | Compare the 8-bit number in E register with value in A register | A3 |
| AND H | Compare the 8-bit number in H register with value in A register | A4 |
| AND L | Compare the 8-bit number in L register with value in A register | A5 |
| AND {??} | Compare the 8-bit immediate number with value in A register | E6 {??} |
| AND (HL) | Compare the 8-bit number at memory address HL with value in A register | A6 |
| AND (IX+displacement) | Compare the 8-bit value in IX with value in A register | DD A6 ?? |
| AND (IY+displacement) | Compare the 8-bit value in IY with value in A register | FD A6 ?? |