Jump to content

Alcor6L/eLua/bit

From Wikibooks, open books for an open world

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

[edit | edit source]

bit.bit

[edit | edit source]

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.isset

[edit | edit source]

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.isclear

[edit | edit source]

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.set

[edit | edit source]

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.clear

[edit | edit source]

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.bnot

[edit | edit source]

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.band

[edit | edit source]

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.bor

[edit | edit source]

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.bxor

[edit | edit source]

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.lshift

[edit | edit source]

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.rshift

[edit | edit source]

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.arshift

[edit | edit source]

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