docs/modules/bit.md
| Since | Origin / Contributor | Maintainer | Source |
|---|---|---|---|
| 2014-12-24 | https://github.com/LuaDist/bitlib, Zeroday | Zeroday | bit.c |
Bit manipulation support, on 32bit integers.
Arithmetic right shift a number equivalent to value >> shift in C.
bit.arshift(value, shift)
value the value to shiftshift positions to shiftthe number shifted right (arithmetically)
bit.arshift(3, 1) -- returns 1
-- Using a 4 bits representation: 0011 >> 1 == 0001
Bitwise AND, equivalent to val1 & val2 & ... & valn in C.
bit.band(val1, val2 [, ... valn])
val1 first AND argumentval2 second AND argument...valn ...nth AND argumentthe bitwise AND of all the arguments (number)
bit.band(3, 2) -- returns 2
-- Using a 4 bits representation: 0011 & 0010 == 0010
Generate a number with a 1 bit (used for mask generation). Equivalent to 1 << position in C.
bit.bit(position)
position position of the bit that will be set to 1
a number with only one 1 bit at position (the rest are set to 0)
bit.bit(4) -- returns 16
Bitwise negation, equivalent to ~value in C.
bit.bnot(value)
value the number to negate
the bitwise negated value of the number
Bitwise OR, equivalent to val1 | val2 | ... | valn in C.
bit.bor(val1, val2 [, ... valn])
val1 first OR argument.val2 second OR argument....valn ...nth OR argumentthe bitwise OR of all the arguments (number)
bit.bor(3, 2) -- returns 3
-- Using a 4 bits representation: 0011 | 0010 == 0011
Bitwise XOR, equivalent to val1 ^ val2 ^ ... ^ valn in C.
bit.bxor(val1, val2 [, ... valn])
val1 first XOR argumentval2 second XOR argument...valn ...nth XOR argumentthe bitwise XOR of all the arguments (number)
bit.bxor(3, 2) -- returns 1
-- Using a 4 bits representation: 0011 ^ 0010 == 0001
Clear bits in a number.
bit.clear(value, pos1 [, ... posn])
value the base numberpos1 position of the first bit to clear...posn position of thet nth bit to clearthe number with the bit(s) cleared in the given position(s)
bit.clear(3, 0) -- returns 2
Test if a given bit is cleared.
bit.isclear(value, position)
value the value to testposition bit position to testtrue if the bit at the given position is 0, false otherwise
bit.isclear(2, 0) -- returns true
Test if a given bit is set.
bit.isset(value, position)
value the value to testposition bit position to testtrue if the bit at the given position is 1, false otherwise
bit.isset(2, 0) -- returns false
Left-shift a number, equivalent to value << shift in C.
bit.lshift(value, shift)
value the value to shiftshift positions to shiftthe number shifted left
bit.lshift(2, 2) -- returns 8
-- Using a 4 bits representation: 0010 << 2 == 1000
Logical right shift a number, equivalent to ( unsigned )value >> shift in C.
bit.rshift(value, shift)
value the value to shift.shift positions to shift.the number shifted right (logically)
bit.rshift(2, 1) -- returns 1
-- Using a 4 bits representation: 0010 >> 1 == 0001
Set bits in a number.
bit.set(value, pos1 [, ... posn ])
value the base number.pos1 position of the first bit to set....posn position of the nth bit to set.the number with the bit(s) set in the given position(s)
bit.set(2, 0) -- returns 3