Alcor6L/eLua/bit

Since Lua doesn't have (yet) built-in capabilities for bit operations, the bit module was added to eLua to fill this gap. It is based on the bitlib library written by Reuben Thomas (slightly adapted to eLua) and provides basic bit operations (like setting and clearing bits) and bitwise operations.

FunctionsEdit

bit.bitEdit

Generate a number with a 1 bit (used for mask generation). Equivalent to 1 << position in C.

number = bit.bit( position )
  • position - position of the bit that will be set to 1.

Returns:

  • number - a number with only one 1 bit at position (the rest are set to 0).

bit.issetEdit

Test if a given bit is set.

flag = bit.isset( value, position )
  • value - the value to test.
  • position - bit position to test.

Returns:

  • flag - true if the bit at the given position is 1, false otherwise.

bit.isclearEdit

Test if a given bit is cleared.

flag = bit.isclear( value, position )
  • value - the value to test.
  • position - bit position to test.

Returns:

  • flag - true if the bit at the given position is 0, false othewise.

bit.setEdit

Set bits in a number.

number = bit.set( value, pos1, pos2, ..., posn )
  • value - the base number.
  • pos1 - position of the first bit to set.
  • pos2 - position of the second bit to set.
  • posn - position of the nth bit to set.

Returns:

  • number - the number with the bit(s) set in the given position(s).

bit.clearEdit

Clear bits in a number.

number = bit.clear( value, pos1, pos2, ..., posn )
  • value - the base number.
  • pos1 - position of the first bit to clear.
  • pos2 - position of the second bit to clear.
  • posn - position of thet nth bit to clear.

Returns: number - the number with the bit(s) cleared in the given position(s).

bit.bnotEdit

Bitwise negation, equivalent to ~value in C.

number = bit.bnot( value )
  • value - the number to negate.

Returns: number - the bitwise negated value of the number.

bit.bandEdit

Bitwise AND, equivalent to val1 & val2 & ... & valn in C.

number = bit.band( val1, val2, ... valn )
  • val1 - first AND argument.
  • val2 - second AND argument.
  • valn - nth AND argument.

Returns: number - the bitwise AND of all the arguments.

bit.borEdit

Bitwise OR, equivalent to val1 | val2 | ... | valn in C.

number = bit.bor( val1, val2, ... valn )
  • val1 - first OR argument.
  • val2 - second OR argument.
  • valn - nth OR argument.

Returns: number - the bitwise OR of all the arguments.

bit.bxorEdit

Bitwise exclusive OR (XOR), equivalent to val1 ^ val2 ^ ... ^ valn in C.

number = bit.bxor( val1, val2, ... valn )
  • val1 - first XOR argument.
  • val2 - second XOR argument.
  • valn - nth XOR argument.

Returns: number - the bitwise exclusive OR of all the arguments.

bit.lshiftEdit

Left-shift a number, equivalent to value < shift in C.

number = bit.lshift( value, shift )
  • value - the value to shift.
  • shift - positions to shift.

Returns: number - the number shifted left

bit.rshiftEdit

Logical right shift a number, equivalent to ( unsigned )value >> shift in C.

number = bit.rshift( value, shift )
  • value - the value to shift.
  • shift - positions to shift.

Returns:

  • number - the number shifted right (logically).

bit.arshiftEdit

Arithmetic right shift a number equivalent to value >> shift in C.

number = bit.arshift( value, shift )
  • value - the value to shift.
  • shift - positions to shift.

Returns:

  • number - the number shifted right (arithmetically).