bfrt_helper.util
Functions
encode_number
- encode_number(number, bitwidth)[source]
Convert an integer to it’s representation in bytes.
gRPC operations involving values are always sent as
bytes
. Since operations internally to the helpers are performed on integers (it is way easier), values need to be ‘casted’ to bytes.The byte string will always be the smallest number of bytes capable of holding the value based upon the bitwidth. If a value is provided that is out of range, an exception will be thrown.
The number of bytes is calculated as:
\[[ (b + 8 - 1) / b ]\]Where \(b\) is the bitwidth.
Conversion to bytes is performed via the
int.to_bytes
method, using a big endian conversion.- Parameters:
number (int) – Value to convert
bitwidth (int) – Bitwidth of the field
- Raises:
ValueOutOfRange – The supplied value exceeds the maximum number the bitwidth is capable of supporting.
bit_not
- bit_not(value, n_bits)[source]
Calculates the bitwise negation (NOT) of an integer.
Since integers in python are both signed and have arbitrary lengths, calculating the bitwsie negation is not straightforward.
The is calculated by:
\[(2^b-1) - x\]Where \(b\) is the number of bits in the value and \(x\) is the value to convert.
Note
This will not work on negative numbers.
- Parameters:
value (int) – The value to convert.
n_bits (int) – The number of bits the value should have.