Skip to content
Textmode edited this page Mar 6, 2011 · 3 revisions

Bitfield is a support library, providing emulated bitwise operations for Lua.

It exposes the following functions:

bitfield:new(opt_num, opt_width)
returns: bitfield

Creates and returns a new bitfield. If opt_num is provided, the initial value of the bitfield will be the binary representation of that number, otherwise it will be zero. If opt_width is provided t will be used as the effective bitwidth of the bitfield, otherwise it will default to 16 bits.

(bf)[n]
returns: bit

returns true if the nth bit is set, otherwise returns false. if a bit outside the range of the bitfield's width is requested, it returns nil instead (which is also logically false)

**(bf)[n] = v **
returns: (nothing)

Sets the nth bit of bf to the truth value of v

(bf):NOT()
returns: num

Negates the value of bf. Additionally returns the resulting value.

NOT 1 -> 0
NOT 0 -> 1

**(bf):AND(b) **
returns: num

ANDs the value of bf with b. Additionally returns the resulting value.

0 AND 0 -> 0
0 AND 1 -> 0
1 AND 0 -> 0
1 AND 1 -> 1

**(bf):OR(b) **
returns: num

ORs the value of bf with b. Additionally returns the resulting value.

0 OR 0 -> 0
0 OR 1 -> 1
1 OR 0 -> 1
1 OR 1 -> 1

**(bf):XOR(b) **
returns: num

XORs the value of bf with b. Additionally returns the resulting value.

0 XOR 0 -> 0
0 XOR 1 -> 1
1 XOR 0 -> 1
1 XOR 1 -> 0

**(bf):NAND(b) **
returns: num

NANDs the value of bf with b. Additionally returns the resulting value.

0 NAND 0 -> 1
0 NAND 1 -> 1
1 NAND 0 -> 1
1 NAND 1 -> 0

**(bf):NOR(b) **
returns: num

NORs the value of bf with b. Additionally returns the resulting value.

0 NOR 0 -> 1
0 NOR 1 -> 0
1 NOR 0 -> 0
1 NOR 1 -> 0

**(bf):XNOR(b) **
returns: num

XNORs the value of bf with b. Additionally returns the resulting value.

0 XNOR 0 -> 1
0 XNOR 1 -> 0
1 XNOR 0 -> 0
1 XNOR 1 -> 1

**(bf):shift(n, sign-ext) **
returns: num

Shifts the value of bf by n. Additionally returns the resulting value.
Positive values shift left, negative values shift right. if sign-ext is true, then the sign bit will be extended during a right-shift.

Clone this wiki locally