Difference between revisions of "Bit.bxor"
From ComputerCraft Wiki
(Rewrite) |
m (Changed nonexistent type int to type number.) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
{{Function | {{Function | ||
|name=bit.bxor | |name=bit.bxor | ||
− | |args= | + | |args={{Type|number}} m, {{Type|number}} n |
|api=bit | |api=bit | ||
− | |returns= | + | |returns={{Type|number}} the value of <var>m</var> XOR <var>n</var> |
|addon=ComputerCraft | |addon=ComputerCraft | ||
|desc=Computes the bitwise exclusive OR of two numbers | |desc=Computes the bitwise exclusive OR of two numbers | ||
Line 55: | Line 55: | ||
| 1 | | 1 | ||
|} | |} | ||
+ | |||
+ | [[Category:API_Functions]] |
Latest revision as of 01:21, 12 July 2013
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
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 |