-
Notifications
You must be signed in to change notification settings - Fork 0
Bitfield
Bitfield is a support library, providing emulated bitwise operations for Lua.
It exposes the following functions:
bitfield:new(opt_num, opt_width)
returns: bitfieldCreates 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: bitreturns 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: numNegates the value of bf. Additionally returns the resulting value.
NOT 1 -> 0
NOT 0 -> 1
**(bf):AND(b) **
returns: numANDs 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: numORs 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: numXORs 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: numNANDs 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: numNORs 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: numXNORs 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: numShifts 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.