Difference between revisions of "Bit.band"
From ComputerCraft Wiki
m (Moved to CAT:APIFunctions) |
m (Mapped "int" to "int (type)") |
||
Line 4: | Line 4: | ||
|args=[[int (type)|int]] m, [[int (type)|int]] n | |args=[[int (type)|int]] m, [[int (type)|int]] n | ||
|api=bit | |api=bit | ||
− | |returns=[[int]] the value of <var>m</var> AND <var>n</var> | + | |returns=[[int (type)|int]] the value of <var>m</var> AND <var>n</var> |
|addon=ComputerCraft | |addon=ComputerCraft | ||
|desc=Computes the bitwise AND of two numbers | |desc=Computes the bitwise AND of two numbers |
Revision as of 18:28, 30 November 2012
Function bit.band | |
Computes the bitwise AND of two numbers | |
Syntax | bit.band(int m, int n) |
Returns | int the value of m AND n |
Part of | ComputerCraft |
API | bit |
Examples
Example | |
AND the number 18 (10010) with the number 3 (00011), yielding 2 (00010) | |
Code |
print(bit.band(18, 3)) |
Output | 2 |
Explanation
All bit operations operate in binary numeral system [1]. An AND operation between two bits yields a 1 if the bits are both 1 and a 0 if either of the bits is 0. This function produces an output by computing the AND 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: | 3 has a 0 | Both 0 | Both 0 | Both 1 | 18 has a 0 |
Output (2): | 0 | 0 | 0 | 1 | 0 |