Bitwise Operators: Difference between revisions

m
→‎XOR: fixed formatting inconsistency
m (→‎XOR: fixed formatting inconsistency)
 
(4 intermediate revisions by the same user not shown)
Line 14:
For all numbers A:
 
* <code>A & 0 = 0</code>
* <code>A & A = A</code>
* <code>A & -1 = A (remember that -1 = FF)</code>
* Every 4 binary digits of A and B can be <code>AND</code>ed separately and concatenated, and the result is the same.
Line 73 ⟶ 74:
For all numbers A:
* <code>A | 0 = A</code>
* <code>A | A = A</code>
* <code>A | -1 = -1</code>
 
 
The first property implies that if a subset of a byte is a sequence of zeroes, then <code>OR</code>ing that sequence of zeroes with any bit combination will result in the same bit combination. For example:
Line 102 ⟶ 105:
* A bit <code>XOR</code>'d with 1 will flip. A bit <code>XOR</code>'d with zero won't change.
* A bitwise <code>NOT</code> is the same as <code>XOR -1</code>.
* <code>A ^ A = 0</code>. Furthermore, if <code>A ^ B = 0</code>, then <code>A = B</code>.
* If <code>((A ^ B) & C) = 0</code>, then A and B both share the bits of C in common.
 
 
Line 110 ⟶ 115:
==NOT==
A bitwise <code>NOT</code>, or <code>~</code> takes one number as an argument and flips all its bits. This has its uses, especially when reading video game controllers, but is a little more limited than the others.
 
Among <code>AND</code>, <code>OR</code>, <code>XOR</code>, and <code>NOT</code>, only <code>XOR</code> and <code>NOT</code> are reversible.
 
 
==Citations==
#[[http://www.6502.org/tutorials/compare_beyond.html Beyond 8-Bit Unsigned Comparisons by Bruce Clark]]
1,489

edits