Difference between revisions of "Bit.bxor"

From ComputerCraft Wiki
Jump to: navigation, search
(Rewrite)
Line 1: Line 1:
== Explanation ==
+
{{lowercase}}
All bit operations operate in Binary numeral system [http://en.wikipedia.org/wiki/Binary_numeral_system].
+
{{Function
 +
|name=bit.bxor
 +
|args=[[int (type)|int]] m, [[int (type)|int]] n
 +
|api=bit
 +
|returns=[[int]] the value of <var>m</var> XOR <var>n</var>
 +
|addon=ComputerCraft
 +
|desc=Computes the bitwise exclusive OR of two numbers
 +
|examples=
 +
{{Example
 +
|desc=XOR the number 18 (10010) with the number 3 (00011), yielding 17 (10001)
 +
|code=print(bit.bxor(18, 3))
 +
|output=17
 +
}}
 +
}}
  
So if you understand the basics than XOR is an EXCLUSIVE OR and like an OR with exclusion give:
+
== Explanation ==
----
+
All bit operations operate in binary numeral system [http://en.wikipedia.org/wiki/Binary_numeral_system]. An exclusive OR operation between two bits yields a 1 if the bits are unequal and a 0 if they are equal. This function produces an output by computing the XOR of each bit of its two inputs independently. So, for the example above:
01001011 (75)
+
----
+
<i>XOR</i>
+
----
+
00011000 (24)
+
----
+
<i>=</i>
+
----
+
01010011 (83)
+
----
+
  
Bits are XOR`red sequentially.
+
{| class="wikitable"
 +
|-
 +
! Bit index:
 +
| 4
 +
| 3
 +
| 2
 +
| 1
 +
| 0
 +
|-
 +
! Input 1 (18):
 +
| 1
 +
| 0
 +
| 0
 +
| 1
 +
| 0
 +
|-
 +
! Input 2 (3):
 +
| 0
 +
| 0
 +
| 0
 +
| 1
 +
| 1
 +
|-
 +
! Calculation:
 +
| 1 ≠ 0
 +
| 0 = 0
 +
| 0 = 0
 +
| 1 = 1
 +
| 0 ≠ 1
 +
|-
 +
! Output (17):
 +
| 1
 +
| 0
 +
| 0
 +
| 0
 +
| 1
 +
|}

Revision as of 22:54, 11 March 2012


Grid Redstone.png  Function bit.bxor
Computes the bitwise exclusive OR of two numbers
Syntax bit.bxor(int m, int n)
Returns int the value of m XOR n
Part of ComputerCraft
API bit

Examples

Grid paper.png  Example
XOR the number 18 (10010) with the number 3 (00011), yielding 17 (10001)
Code
print(bit.bxor(18, 3))
Output 17


Explanation

All bit operations operate in binary numeral system [1]. An exclusive OR operation between two bits yields a 1 if the bits are unequal and a 0 if they are equal. This function produces an output by computing the XOR of each bit of its two inputs independently. So, for the example above:

Bit index: 4 3 2 1 0
Input 1 (18): 1 0 0 1 0
Input 2 (3): 0 0 0 1 1
Calculation: 1 ≠ 0 0 = 0 0 = 0 1 = 1 0 ≠ 1
Output (17): 1 0 0 0 1