Difference between revisions of "Bit.bxor"

From ComputerCraft Wiki
Jump to: navigation, search
m (Above 1 and Below 0)
m (Changed nonexistent type int to type number.)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
{{lowercase}}
 +
{{Function
 +
|name=bit.bxor
 +
|args={{Type|number}} m, {{Type|number}} n
 +
|api=bit
 +
|returns={{Type|number}} 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
 +
}}
 +
}}
 +
 
== Explanation ==
 
== Explanation ==
''XOR'' is similar to the ''OR'' gate, but the numbers can '''not''' be equal.
+
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:
(ex. 0<u>V</u>0=0; 1<u>V</u>0=1; 0<u>V</u>1=1; 1<u>V</u>1=0)
+
  
== Above 1 and Below 0 ==
+
{| class="wikitable"
I tried to research and work on numbers above 1, but it gets kind of confusing, at first it is simple(ex. (1,2)=3; (2,2)=0; (4,2)=6; (5,2)=7;), but then when you input (6,2) it equals 4. I tried working on this for half and hour with no luck. When I tried to work on numbers below 0 at first it was as confusing as above 1 and as simple as 0 and 1, but still a positive number. Then when I mixed negative and positive, I got 4294967295 from (-1,1). I didn't try to research it. So hopefully someone will edit this and explain how going above 1 works, and possibly include an equation or 2.
+
|-
 +
! 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
 +
|}
  
--[[User:Zach1231|Zach1231]] 23:59, 26 February 2012 (UTC)
+
[[Category:API_Functions]]

Latest revision as of 01:21, 12 July 2013


Grid Redstone.png  Function bit.bxor
Computes the bitwise exclusive OR of two numbers
Syntax bit.bxor(number m, number n)
Returns number 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