We're looking for your comments on how to best organize the wiki's content.

Boolean algebra

From 0x10c Wiki
Jump to: navigation, search

Boolean values are true and false values stored as binary bits 1 and 0 respectively. In the same way that integers have operations such as addition and subtraction, boolean values have the operators of and, or, and exclusive or.

Contents

[edit] Boolean Operators

[edit] AND (&)

A & B is true if and only if A is true and B is true.

AND 1 0
1 1 0
0 0 0
  1001 1011  A
& 0010 1001  B
-----------
  0000 1001  A&B

[edit] BOR (|)

A | B is true if A is true, B is true, or both are true.

BOR 1 0
1 1 1
0 1 0
  1001 1011  A
| 0010 1001  B
-----------
  1011 1011  A|B

[edit] XOR (^)

A ^ B is true if and only if A is true and B is false OR A is false and B is true. In other words, A ^ B is true when A and B are different.

XOR 1 0
1 0 1
0 1 0
  1001 1011  A
^ 0010 1001  B
-----------
  1011 0010  A^B

[edit] Shifting

The Shift Left (SHL, <<) and Shift Right (SHR, >>) push all of the bits to the left and right, respectively. The bits that go beyond the boundary of a 16-bit word will be placed in the Excess (EX) register.

   SET A, 0x670F
   ; A = 0110 0111 0000 1111
   SHL A, 3
   ; A = 0011 1000 0111 1000
   ; EX = 0000 0000 0000 0110
   SHR A, 6
   ; A = 0000 0000 1110 0001
   ; EX = 1110 0000 0000 0000

A shifting operation has the effect of multiplying (left), or dividing (right) by 2.

[edit] Using Boolean Algebra in Code

[edit] The IFB Conditional Opcode

The IFB conditional will execute the next instruction if A&B != 0. This is to say that if any of the bits between A and B are are both 1, then the next operation will be set. This is useful when checking for bit flags. For example, let's say you have a variable where the highest bit means to run a certain subroutine.

   IFB A, 0x8000    ; If the highest bit is true
   JSR something    ; Perform a subroutine
   ...              ; other code here

[edit] The IFC Conditional Opcode

The IFC conditional is very similar to IFB in that it tests A&B, but it will only execute the next instruction if A&B == 0. That is, if none of the bit positions are 1 in both A and B, the next instruction is executed. It is effectively the opposite of IFB.

[edit] Bitmasks

In some cases, you may wish to try to pack as much information into a single word as possible. Let's say instead of wanting to store a single 16-bit value in a register, you wish to use it to store two 8-bit values. For this, you can create a mask that has 1's in the bits that represent each component. So the mask for one value would be 0xff00, while the other would be 0x00ff. AND-ing a value with a mask will erase all irrelevant bits, leaving the ones that are part of the mask.

      A = 1101 0011 0110 0001   Two 8-bit values
& mask1 = 1111 1111 0000 0000
          -------------------
          1101 0011             The first value. Shift right 8 bits to save it.

      A = 1101 0011 0110 0001
& mask2 = 0000 0000 1111 1111
          -------------------
                    0110 0001   The second value.
    ; Lets say A holds two 8-bit values. We want to put the first in B and the second in C.
    SET B, A
    AND B, 0xff00 ; AND the mask.
    SHR B, 8      ; Shift the bits to the right.
 
    SET C, A
    AND C, 0x00ff ; AND the mask.

[edit] Inverting Bits

XOR is a special command in that any value XOR 1 is equal to the opposite value. Therefore, it's possible to flip every bit in a register by XOR-ing it with 0xffff.

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox