Awkl
Loading...
Searching...
No Matches
bit Namespace Reference

Enumerations

enum  SGN { SGN_UINT = 0 , SGN_2COM = 1 , SGN_MAGN = 2 , SGN_1COM = 3 }
 Interpretation of a bit sequence. More...
 

Functions

number and (number x, number y, number w)
 Bitwise AND operator.
 
number or (number x, number y, number w)
 Bitwise OR operator.
 
number xor (number x, number y, number w)
 Bitwise XOR or exclusive or operator.
 
number compl (number x, number w)
 Bitwise NOT or one's complement operator.
 
number lshift (number x, number n, number w)
 Left shift.
 
number rshift (number x, number n, number w)
 Arithmetic right shift.
 
number lrot (number x, number n, number w=64)
 Left rotate.
 
number rrot (number x, number n, number w=64)
 Right rotate.
 
number lrshift (number x, number n)
 Logical right shift.
 
number isBitSet (number x, number p)
 Check if a bit is set at a specific position.
 
number setBit (number x, number p)
 Set a bit at a specific position.
 
number clearBit (number x, number p, number w=64)
 Clear a bit at a specific position.
 
number toggleBit (number x, number p)
 Toggle a bit at a specific position.
 
number countSetBits (number x)
 Count total number of bits set.
 
number bitSeq (string s, enum SGN f)
 Bit string literal to integer.
 

Enumeration Type Documentation

◆ SGN

enum bit::SGN

Interpretation of a bit sequence.

Enumerator
SGN_UINT 

Unsigned integer. Default.

SGN_2COM 

Signed integer with two's complement.

SGN_MAGN 

Signed integer with sign-magnitude.

SGN_1COM 

Signed integer with one's complement.

Function Documentation

◆ and()

number bit::and ( number x,
number y,
number w )

Bitwise AND operator.

Parameters
xLeft operand
yRight operand
wBit-width being used
Returns
Result of logical AND operation on each pair of corresponding bits of the operands

◆ bitSeq()

number bit::bitSeq ( string s,
enum SGN f )

Bit string literal to integer.

Parameters
sString representation of a bit sequence, composed of 0s and 1s optionally prefixed with 0b
fInterpretation of the bit sequence
Returns
Interpreted bit sequence

◆ clearBit()

number bit::clearBit ( number x,
number p,
number w = 64 )

Clear a bit at a specific position.

Parameters
xNumber to clear its bit
pPosition of the bit to clear (0-based index)
wMinimum bit-width required
Returns
Number with a bit cleared at the specified position

◆ compl()

number bit::compl ( number x,
number w )

Bitwise NOT or one's complement operator.

Parameters
xNumber to compute its w-bit complement
wBit-width being used
Returns
Result of logical NOT operation on each pair of corresponding bits of the operands, also known as its one's complement
Note
This differs from the builtin awk::compl in that it can accept a negative argument, a bit-width parameter and the result is interpreted as a signed integer instead of an unsigned similar to languages like shell, JavaScript and C

◆ countSetBits()

number bit::countSetBits ( number x)

Count total number of bits set.

Parameters
xNumber to count its bits
Returns
Count of the total number of bits set

◆ isBitSet()

number bit::isBitSet ( number x,
number p )

Check if a bit is set at a specific position.

Parameters
xNumber to check its bit
pPosition of the bit to check (0-based index)
Returns
1 if bit is set at the specified position, 0 otherwise

◆ lrot()

number bit::lrot ( number x,
number n,
number w = 64 )

Left rotate.

Parameters
xNumber to rotate its bits
nNumber of bits to rotate to the left
wBit-width being rotated on
Returns
Number with its bits rotated to the left

◆ lrshift()

number bit::lrshift ( number x,
number n )

Logical right shift.

Parameters
xNumber to logically shift its bits
nNumber of bits to logically shift to the right
Returns
Number with its bits logically shifted to the right
Note
64-bit width assumed
Todo
Bit width parameter
Bug
awk::and doesn't allow negative arguments

◆ lshift()

number bit::lshift ( number x,
number n,
number w )

Left shift.

Parameters
xNumber to shift its bits
nNumber of bits to shift to the left
wBit-width being shifted on
Returns
Number with its bits shifted to the left

◆ or()

number bit::or ( number x,
number y,
number w )

Bitwise OR operator.

Parameters
xLeft operand
yRight operand
wBit-width being used
Returns
Result of logical OR operation on each pair of corresponding bits of the operands

◆ rrot()

number bit::rrot ( number x,
number n,
number w = 64 )

Right rotate.

Parameters
xNumber to rotate its bits
nNumber of bits to rotate to the right
wBit-width being rotated on
Returns
Number with its bits rotated to the right

◆ rshift()

number bit::rshift ( number x,
number n,
number w )

Arithmetic right shift.

Parameters
xNumber to shift its bits
nNumber of bits to arithmetically shift to the right
wBit-width being shifted on
Returns
Number with its bits arithmetically shifted to the right
Bug
Some edge cases when -n is close to -w or x is close to -2^w

◆ setBit()

number bit::setBit ( number x,
number p )

Set a bit at a specific position.

Parameters
xNumber to set its bit
pPosition of the bit to set (0-based index)
Returns
Number with a bit set at the specified position

◆ toggleBit()

number bit::toggleBit ( number x,
number p )

Toggle a bit at a specific position.

Parameters
xNumber to toggle its bits
pPosition of the bit to toggle
Returns
Number with a bit toggled at the specified position

◆ xor()

number bit::xor ( number x,
number y,
number w )

Bitwise XOR or exclusive or operator.

Parameters
xLeft operand
yRight operand
wBit-width being used
Returns
Result of logical exclusive OR operation on each pair of corresponding bits of the operands