eLua reference manual - bit module

Overview

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.

Functions

number = bit.bit( position )

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

Arguments: 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).

flag = bit.isset( value, position )

Test if a given bit is set.

Arguments:

  • value - the value to test.
  • position - bit position to test.

Returns: boolean - true if the bit at the given position is 1, false otherwise.

flag = bit.isclear( value, position )

Test if a given bit is cleared.

Arguments:

  • value - the value to test.
  • position - bit position to test.

Returns: boolean - true if the bit at the given position is 0, false othewise.

number = bit.set( value, pos1, pos2, ..., posn )

Set bits in a number.

Arguments:

  • 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).

number = bit.clear( value, pos1, pos2, ..., posn )

Clear bits in a number.

Arguments:

  • 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).

number = bit.bnot( value )

Bitwise negation, equivalent to ~value in C.

Arguments: value - the number to negate.

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

number = bit.band( val1, val2, ... valn )

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

Arguments:

  • val1 - first AND argument.
  • val2 - second AND argument.
  • valn - nth AND argument.

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

number = bit.bor( val1, val2, ... valn )

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

Arguments:

  • val1 - first OR argument.
  • val2 - second OR argument.
  • valn - nth OR argument.

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

number = bit.bxor( val1, val2, ... valn )

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

Arguments:

  • val1 - first XOR argument.
  • val2 - second XOR argument.
  • valn - nth XOR argument.

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

number = bit.lshift( value, shift )

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

Arguments:

  • value - the value to shift.
  • shift - positions to shift.

Returns: number - the number shifted left

number = bit.rshift( value, shift )

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

Arguments:

  • value - the value to shift.
  • shift - positions to shift.

Returns: number - the number shifted right (logically).

number = bit.arshift( value, shift )

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

Arguments:

  • value - the value to shift.
  • shift - positions to shift.

Returns: number - the number shifted right (arithmetically).