LuaCheia Logo  

LuaCheia Reference Manual

bit

Synopsis

cheia.load “bit”
 
r = bit.bnot(x)
r = bit.band(a, b, ...)
r = bit.bor(a, b, ...)
r = bit.bxor(a, b, ...)
r = bit.lshift(x, places)
r = bit.rshift(x, places)
r = bit.arshift(x, places)
r = bit.mod(numerator, denominator)

Description

This module provides a selection of bitwise operations.

Warning: Beware of precision. Lua stores all numbers in double-precision floating-point format (by default). This means that large numbers are not stored accurately. Therefore, the functions in this module should not be expected to behave correctly for numbers exceeding 32 bits (although 53 bits are probably safe at least on i386). This is of particular consequence for the bit.lshift operation: the most significant bits are not discarded, but shifted with the rest, which may cause the number to grow large enough that precision is lost for the least significant bits. For this reason, a bit.lshift should always be preceded by a bit.mod to discard the unwanted bits when there is any danger of the number growing too large.

Note: The fractional part of any operand is discarded. The right shift operations do not move bits into the fractional part.

Examples

To clear bit n (0-based) of number x:

x = bit.band(bit.bnot(bit.lshift(1, n)), x)

To retrieve the high and low bytes of unsigned 16-bit number x:

high = bit.rshift(x, 8)
low = bit.mod(x, 256)

Reference

All operations are directly implemented using their C equivalents operating on long longs.

bit.bnot(x)

Perforns a bitwise NOT of its operand. Equivalent to ~ in C.

Parameters:
     x : number  

Operand.

Returns:
     number  

Result.

bit.band(...)

Performs a bitwise AND of its operands. Equivalent to & in C.

Parameters:
     ... : numbers  

Two or more operands.

Returns:
     number  

Result.

bit.bor(...)

Performs a bitwise OR of its operands. Equivalent to | in C.

Parameters:
     ... : numbers  

Two or more operands.

Returns:
     number  

Result.

bit.bxor(...)

Performs a bitwise XOR of its operands. Equivalent to ^ in C.

Parameters:
     ... : numbers  

Two or more operands.

Returns:
     number  

Result.

bit.lshift(x, n)

Performs a bitwise left shift of its operand. Equivalent to << in C. See the warning above.

Parameters:
     x : number  

Operand.

     n : number  

Number of places to shift.

bit.rshift(x, n)

Performs an unsigned bitwise right shift of its operand. The result is always positive. Equivalent to >> in C.

Parameters:
     x : number  

Operand.

     n : number  

Number of places to shift.

bit.arshift(x, n)

Performs a bitwise right shift of its operand, but the sign of the operand is preserved. Equivalent to >> in C.

Parameters:
     x : number  

Operand.

     n : number  

Number of places to shift.

bit.mod(n, d)

Returns the remainder of an integer division of n by d. Equivalent to % in C.

Parameters:
     n : number  

Numerator.

     d : number  

Denominator.

Issues

Should the module automatically clip numbers to within the ‘safe’ range?

Since all ‘safe’ numbers fit within a signed long long, there seems no need to distinguish bit.rshift and bit.arshift.

See also

None.

Revision history

Added in LuaCheia 5.0.

Credits

Lua module by Reuben Thomas.

Modifications for LuaCheia by Thatcher Ulrich.

Documentation for LuaCheia by Jamie Webb.

Return to main site

Introduction

 » Writing LuaCheia modules

Module Reference

 » bit
 » cgi
 » fuzzy
 » md5
 » pack
 » rex
 » SDL
 » shelve
 » sqlite

Appendices

 » Module path conventions
 » Module names
 » Application Binary Interface
 » Credits