dreamworkAPI
Ctrl K
GitHub
libraryDocs / bitpack

bitpack

librarysharedmenusource

Library that packs & unpacks bits in integers and bytes.

Bit positions are 0-indexed and counted from the least significant bit, so position 0 is the lowest bit (0x01) and position 7 is the highest bit of a byte (0x80), same as bit.extract and pack.

Field functions (get, set, toggle, readUInt, readInt, write) work on any integer up to 32 bits, not only on bytes, because they are made of 32-bit shifts: readUInt( x, position, bit_count ) shifts the field to the top of 32 bits (bits above it fall off), then shifts it down to bit 0 (bits below it fall off).

So fields that cross byte boundaries are read and written by joining bytes into an integer with bytepack first:

lua
-- [15] flag_a [14] flag_b [13..0] int14
local x = bytepack.readUInt16BE( uint8_1, uint8_2 )

local flag_a = bitpack.get( x, 15 )
local flag_b = bitpack.get( x, 14 )
local value = bitpack.readInt( x, 0, 14 )

x = bitpack.write( x, 0, 14, value - 1 )
uint8_1, uint8_2 = bytepack.writeUInt16BE( x )

Integers are taken modulo 2 ^ 32, a field must fit in them (position + bit_count <= 32), and results with bit 31 set are negative (signed 32-bit), except readUInt.

Byte functions (reverse, count, findFirst, findLast, rotate, swapNibbles, pack, unpack, toString, fromString) work on the lowest 8 bits only.

Bit sequences are tables of booleans, stored LSB-first by default (bits[ 1 ] is bit 0 of the first byte), or MSB-first when msb_first is true (bits[ 1 ] is bit 7 of the first byte, the order bits are usually written in).

Functions

bitpack.countCounts set bits in the byte (population count).
bitpack.findFirstFinds the position of the lowest set bit in the byte.
bitpack.findLastFinds the position of the highest set bit in the byte.
bitpack.fromStringReads the byte from a string of 0s and 1s, from the highest bit to the lowest.
bitpack.fromTablePacks the sequence of bits (booleans) into the sequence of bytes, unused bits of the last byte are 0.
bitpack.getChecks if the bit at the given position of the integer is set.
bitpack.packPacks up to 8 booleans into the byte, from the lowest bit to the highest, nil is false.
bitpack.readIntReads signed (two's complement) integer from the bit field of the integer.
bitpack.readUIntReads unsigned integer from the bit field of the integer.
bitpack.reverseReverses the order of bits in the byte.
bitpack.rotateRotates bits of the byte, bits shifted out of one end come back on the other.
bitpack.setSets the bit at the given position of the integer.
bitpack.swapNibblesSwaps the lower and upper 4 bits (nibbles) of the byte.
bitpack.toggleFlips the bit at the given position of the integer.
bitpack.toStringConverts the byte to a string of 0s and 1s, from the highest bit to the lowest.
bitpack.toTableUnpacks the sequence of bytes into the sequence of bits (booleans).
bitpack.unpackUnpacks the byte as 8 booleans, from the lowest bit to the highest.
bitpack.writeWrites unsigned or signed (two's complement) integer into the bit field of the integer, bits outside of the field are l…

#bitpack.count

sharedmenunodiscardsource
function bitpack.count(byte: integer): integer

Counts set bits in the byte (population count).

lua
count( 0b01101100 ) --> 4
Parameters
NameTypeDescription
byteintegerThe byte.
Returns
TypeNameDescription
integercountThe number of 1 bits (0 - 8).

#bitpack.findFirst

sharedmenunodiscardsource
function bitpack.findFirst(byte: integer): integer | nil

Finds the position of the lowest set bit in the byte.

lua
findFirst( 0b01101100 ) --> 2
findFirst( 0 ) --> nil
Parameters
NameTypeDescription
byteintegerThe byte.
Returns
TypeNameDescription
integer | nilpositionThe bit position (0 - 7), or nil if the byte is 0.

#bitpack.findLast

sharedmenunodiscardsource
function bitpack.findLast(byte: integer): integer | nil

Finds the position of the highest set bit in the byte.

lua
findLast( 0b01101100 ) --> 6
findLast( 0 ) --> nil
Parameters
NameTypeDescription
byteintegerThe byte.
Returns
TypeNameDescription
integer | nilpositionThe bit position (0 - 7), or nil if the byte is 0.

#bitpack.fromString

sharedmenunodiscardsource
function bitpack.fromString(str: string): integer | nil

Reads the byte from a string of 0s and 1s, from the highest bit to the lowest.

lua
fromString( "00000101" ) --> 5
fromString( "101" ) --> 5
fromString( "2" ) --> nil
Parameters
NameTypeDescription
strstringThe string of 0s and 1s, up to 8 characters.
Returns
TypeNameDescription
integer | nilbyteThe byte, or nil if the string is empty, too long or has other characters.

#bitpack.fromTable

sharedmenunodiscardsource
function bitpack.fromTable(bits: boolean[], bit_count?: integer, msb_first?: boolean): integer[], integer

Packs the sequence of bits (booleans) into the sequence of bytes, unused bits of the last byte are 0.

Parameters
NameTypeDescription
bitsboolean[]The sequence of bits.
bit_countoptionalintegerThe number of bits.
msb_firstoptionalbooleantrue if the highest bit of each byte is stored first, false by default.
Returns
TypeNameDescription
integer[]bytesThe sequence of bytes.
integerbyte_countThe number of bytes.

#bitpack.get

sharedmenunodiscardsource
function bitpack.get(x: integer, position: integer): boolean

Checks if the bit at the given position of the integer is set.

lua
get( 0b00000100, 2 ) --> true
get( 0x8000, 15 ) --> true
Parameters
NameTypeDescription
xintegerThe integer (up to 32 bits).
positionintegerThe bit position (0 - 31).
Returns
TypeNameDescription
booleanis_settrue if the bit is 1, false if it is 0.

#bitpack.pack

sharedmenunodiscardsource
function bitpack.pack(bit0?: boolean, bit1?: boolean, bit2?: boolean, bit3?: boolean, bit4?: boolean, bit5?: boolean, bit6?: boolean, bit7?: boolean): integer

Packs up to 8 booleans into the byte, from the lowest bit to the highest, nil is false.

lua
pack( true, false, true ) --> 0b00000101
Parameters
NameTypeDescription
bit0optionalboolean
bit1optionalboolean
bit2optionalboolean
bit3optionalboolean
bit4optionalboolean
bit5optionalboolean
bit6optionalboolean
bit7optionalboolean
Returns
TypeNameDescription
integerbyteThe byte.

#bitpack.readInt

sharedmenunodiscardsource
function bitpack.readInt(x: integer, position?: integer, bit_count?: integer): integer

Reads signed (two's complement) integer from the bit field of the integer.

lua
readInt( 0b01101100, 2, 4 ) --> -5
readInt( 0xBB2E, 0, 14 ) --> -1234
Parameters
NameTypeDescription
xintegerThe integer (up to 32 bits).
positionoptionalintegerThe position of the lowest bit of the field (0 - 31), 0 by default.
bit_countoptionalintegerThe size of the field in bits (1 - 32), 1 by default.
Returns
TypeNameDescription
integervalueThe signed integer.

#bitpack.readUInt

sharedmenunodiscardsource
function bitpack.readUInt(x: integer, position?: integer, bit_count?: integer): integer

Reads unsigned integer from the bit field of the integer.

lua
readUInt( 0b01101100, 2, 4 ) --> 0b1011 ( 11 )
readUInt( 0xBB2E, 0, 14 ) --> 15150
Parameters
NameTypeDescription
xintegerThe integer (up to 32 bits).
positionoptionalintegerThe position of the lowest bit of the field (0 - 31), 0 by default.
bit_countoptionalintegerThe size of the field in bits (1 - 32), 1 by default.
Returns
TypeNameDescription
integervalueThe unsigned integer.

#bitpack.reverse

sharedmenunodiscardsource
function bitpack.reverse(byte: integer): integer

Reverses the order of bits in the byte.

lua
reverse( 0b00000110 ) --> 0b01100000
Parameters
NameTypeDescription
byteintegerThe byte.
Returns
TypeNameDescription
integerbyteThe reversed byte.

#bitpack.rotate

sharedmenunodiscardsource
function bitpack.rotate(byte: integer, shift: integer): integer

Rotates bits of the byte, bits shifted out of one end come back on the other.

lua
rotate( 0b10000001, 1 ) --> 0b00000011
rotate( 0b10000001, -1 ) --> 0b11000000
Parameters
NameTypeDescription
byteintegerThe byte.
shiftintegerThe number of bits to rotate by, positive to the left (towards the highest bit), negative to the right.
Returns
TypeNameDescription
integerbyteThe rotated byte.

#bitpack.set

sharedmenunodiscardsource
function bitpack.set(x: integer, position: integer, value?: ( boolean | 0 | 1 )?): integer

Sets the bit at the given position of the integer.

lua
set( 0b00000000, 2, true ) --> 0b00000100
set( 0b11111111, 0, false ) --> 0b11111110
Parameters
NameTypeDescription
xintegerThe integer (up to 32 bits).
positionintegerThe bit position (0 - 31).
valueoptional( boolean | 0 | 1 )?The new value of the bit, true by default.
Returns
TypeNameDescription
integerxThe changed integer.

#bitpack.swapNibbles

sharedmenunodiscardsource
function bitpack.swapNibbles(byte: integer): integer

Swaps the lower and upper 4 bits (nibbles) of the byte.

lua
swapNibbles( 0xA5 ) --> 0x5A
Parameters
NameTypeDescription
byteintegerThe byte.
Returns
TypeNameDescription
integerbyteThe swapped byte.

#bitpack.toggle

sharedmenunodiscardsource
function bitpack.toggle(x: integer, position: integer): integer

Flips the bit at the given position of the integer.

lua
toggle( 0b00000101, 2 ) --> 0b00000001
Parameters
NameTypeDescription
xintegerThe integer (up to 32 bits).
positionintegerThe bit position (0 - 31).
Returns
TypeNameDescription
integerxThe changed integer.

#bitpack.toString

sharedmenunodiscardsource
function bitpack.toString(byte: integer, bit_count?: integer): string

Converts the byte to a string of 0s and 1s, from the highest bit to the lowest.

lua
toString( 5 ) --> "00000101"
toString( 5, 4 ) --> "0101"
Parameters
NameTypeDescription
byteintegerThe byte.
bit_countoptionalintegerThe number of the lowest bits to convert (1 - 8), 8 by default.
Returns
TypeNameDescription
stringstrThe string of 0s and 1s.

#bitpack.toTable

sharedmenunodiscardsource
function bitpack.toTable(bytes: integer[], byte_count?: integer, msb_first?: boolean): boolean[], integer

Unpacks the sequence of bytes into the sequence of bits (booleans).

Parameters
NameTypeDescription
bytesinteger[]The sequence of bytes.
byte_countoptionalintegerThe number of bytes.
msb_firstoptionalbooleantrue to store the highest bit of each byte first, false by default.
Returns
TypeNameDescription
boolean[]bitsThe sequence of bits.
integerbit_countThe number of bits.

#bitpack.unpack

sharedmenunodiscardsource
function bitpack.unpack(byte: integer): boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean

Unpacks the byte as 8 booleans, from the lowest bit to the highest.

lua
unpack( 0b00000101 ) --> true, false, true, false, false, false, false, false
Parameters
NameTypeDescription
byteintegerThe byte.
Returns
TypeNameDescription
booleanbit0
booleanbit1
booleanbit2
booleanbit3
booleanbit4
booleanbit5
booleanbit6
booleanbit7

#bitpack.write

sharedmenunodiscardsource
function bitpack.write(x: integer, position: integer, bit_count: integer, value: integer): integer

Writes unsigned or signed (two's complement) integer into the bit field of the integer, bits outside of the field are left unchanged.

Only the lowest bit_count bits of the value are written, for negative values these bits already are their two's complement, so the same field can be read back with readUInt or readInt.

lua
write( 0b00000011, 2, 4, 11 ) --> 0b00101111
write( 0b00000011, 2, 4, -5 ) --> 0b00101111
write( 0x8000, 0, 14, -1234 ) --> 0xBB2E
Parameters
NameTypeDescription
xintegerThe integer (up to 32 bits).
positionintegerThe position of the lowest bit of the field (0 - 31).
bit_countintegerThe size of the field in bits (1 - 32).
valueintegerThe unsigned or signed integer.
Returns
TypeNameDescription
integerxThe changed integer.
Generated from the dreamwork meta branch.