Bit manipulation

From Retrosoftware

(Difference between revisions)
Jump to: navigation, search
(GOD DAMM IT, WHEN I SAY CODE, IT EXPECT MONO-FUCKING-SPACED CODE!!!!!)
Current revision (21:48, 31 July 2010) (edit) (undo)
m (Changed F***ed up code markup to tt markup.)
 
Line 1: Line 1:
== Setting Clearing and Copying bits of data ==
== Setting Clearing and Copying bits of data ==
-
AND xx will clear the bits in A that are also clear in xx, for example:
+
<tt>AND xx</tt> will clear the bits in A that are also clear in xx, for example:
-
<code>
+
<tt>
A xx after AND xx
A xx after AND xx
%abcdefgh %01010101 %0b0d0f0h
%abcdefgh %01010101 %0b0d0f0h
-
</code>
+
</tt>
-
ORA xx will set the bits in A that are also set in xx, for example:
+
<tt>ORA xx</tt> will set the bits in A that are also set in xx, for example:
-
<code>
+
<tt>
A xx after ORA xx
A xx after ORA xx
%abcdefgh %01010101 %a1c1e1g1
%abcdefgh %01010101 %a1c1e1g1
-
</code>
+
</tt>
-
EOR xx will toggle the bits in A that are set in xx, for example:
+
<tt>EOR xx</tt> will toggle the bits in A that are set in xx, for example:
-
<code>
+
<tt>
A xx after EOR xx
A xx after EOR xx
%abcdefgh %01010101 %aBcDeFgH
%abcdefgh %01010101 %aBcDeFgH
-
</code>
+
</tt>
-
To clear the bits in A that are ''set'' in xx, use both ORA and EOR, for
+
To clear the bits in A that are ''set'' in xx, use both <tt>ORA</tt> and <tt>EOR</tt>, for
example:
example:
-
<code>
+
<tt>
A xx after OR xx after EOR xx
A xx after OR xx after EOR xx
%abcdefgh %01010101 %a1c1e1g1 %a0c0e0g0
%abcdefgh %01010101 %a1c1e1g1 %a0c0e0g0
-
</code>
+
</tt>
You can copy a number of bits to a memory location without changing the
You can copy a number of bits to a memory location without changing the
-
other bits using EOR and AND. For example, to copy the top four bits of A
+
other bits using <tt>EOR</tt> and <tt>AND</tt>. For example, to copy the top four bits of A
into a memory location without changing the bottom four bits, use the
into a memory location without changing the bottom four bits, use the
following:
following:
-
<code>
+
<tt>
A=12345678 dst=abcdefgh
A=12345678 dst=abcdefgh
EOR dst ******** abcdefgh
EOR dst ******** abcdefgh
Line 36: Line 36:
EOR dst 1234efgh abcdefgh
EOR dst 1234efgh abcdefgh
STA dst 1234efgh 1234efgh
STA dst 1234efgh 1234efgh
-
</code>
+
</tt>
This is much more efficient than the usual code:
This is much more efficient than the usual code:
-
<code>
+
<tt>
PHA
PHA
LDA dst:AND #&0F:STA dst
LDA dst:AND #&0F:STA dst
PLA
PLA
AND #&F0:ORA dst:STA dst
AND #&F0:ORA dst:STA dst
-
</code>
+
</tt>
See http://mdfs.net/Info/Comp/6502/ProgTips
See http://mdfs.net/Info/Comp/6502/ProgTips

Current revision

Setting Clearing and Copying bits of data

AND xx will clear the bits in A that are also clear in xx, for example:

      A            xx      after AND xx
  %abcdefgh    %01010101    %0b0d0f0h

ORA xx will set the bits in A that are also set in xx, for example:

      A            xx      after ORA xx
  %abcdefgh    %01010101    %a1c1e1g1

EOR xx will toggle the bits in A that are set in xx, for example:

      A            xx      after EOR xx
  %abcdefgh    %01010101    %aBcDeFgH

To clear the bits in A that are set in xx, use both ORA and EOR, for example:

      A            xx      after OR xx  after EOR xx
  %abcdefgh    %01010101    %a1c1e1g1    %a0c0e0g0

You can copy a number of bits to a memory location without changing the other bits using EOR and AND. For example, to copy the top four bits of A into a memory location without changing the bottom four bits, use the following:

               A=12345678  dst=abcdefgh
   EOR dst       ********      abcdefgh
   AND #&F0      ****0000      abcdefgh
   EOR dst       1234efgh      abcdefgh
   STA dst       1234efgh      1234efgh

This is much more efficient than the usual code:

   PHA
   LDA dst:AND #&0F:STA dst
   PLA
   AND #&F0:ORA dst:STA dst

See http://mdfs.net/Info/Comp/6502/ProgTips