Bitwise Operators: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) No edit summary |
Puppydrum64 (talk | contribs) No edit summary |
||
Line 13: | Line 13: | ||
</pre> |
</pre> |
||
A few properties of AND that aren't readily apparent. These assume an 8-bit architecture but also apply to other architectures as well if you add enough trailing ones to fill the variable size. (8 bit was chosen for brevity's sake.) |
A few properties of <code>AND</code> that aren't readily apparent. These assume an 8-bit architecture but also apply to other architectures as well if you add enough trailing ones to fill the variable size. (8 bit was chosen for brevity's sake.) |
||
For all numbers A: |
For all numbers A: |
||
Line 40: | Line 40: | ||
===Random Chance=== |
===Random Chance=== |
||
Given the output of a random number generator with all equally likely outcomes, you can use AND with that output to create random occurrences. If <math>n</math> is a power of 2, then to create a random event with probability <math>1/n</math>, you just need to AND the RNG output with <math>n-1</math>. In the example below, <code>rand</code> is a randomly generated 8-bit value. |
Given the output of a random number generator with all equally likely outcomes, you can use AND with that output to create random occurrences. If <math>n</math> is a power of 2, then to create a random event with probability <math>1/n</math>, you just need to <code>AND</code> the RNG output with <math>n-1</math>. In the example below, <code>rand</code> is a randomly generated 8-bit value. |
||
<lang C> |
<lang C> |
||
Line 62: | Line 62: | ||
==OR== |
==OR== |
||
Bitwise OR, often abbreviated to <code>|</code> (SHIFT+\), compares each individual bit of two different numbers, and outputs a 0 if both are 0, and a 1 otherwise. |
Bitwise <code>OR</code>, often abbreviated to <code>|</code> (SHIFT+\), compares each individual bit of two different numbers, and outputs a 0 if both are 0, and a 1 otherwise. |
||
<pre> |
<pre> |
||
10101101 |
10101101 |
||
Line 92: | Line 92: | ||
==XOR== |
==XOR== |
||
eXclusive OR, often abbreviated to <code>^</code> or called <code>EOR</code> in some languages, compares each individual bit of two different numbers, and outputs a 1 if they are different and 0 if they are the same. |
eXclusive OR, often abbreviated to <code>XOR</code>, <code>^</code> or called <code>EOR</code> in some languages, compares each individual bit of two different numbers, and outputs a 1 if they are different and 0 if they are the same. |
||
<pre> |
<pre> |
||
10101101 |
10101101 |
||
Line 103: | Line 103: | ||
* If <code>A XOR B = C</code>, then <code>A XOR C = B</code> and <code>B XOR C = A</code>. |
* If <code>A XOR B = C</code>, then <code>A XOR C = B</code> and <code>B XOR C = A</code>. |
||
* A bit <code>XOR</code>'d with 1 will flip. A bit <code>XOR</code>'d with zero won't change. |
* 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>. |
|||
==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. |
|||
===Toggling=== |
===Toggling=== |