utils.py
created by Eduardo Diaz
This module defines all the common utils needed in order to make programming easier
Returns a bitset range converted to a number
Usage:
bitsetRangeToNum(bitset[5:8])
Returns number of bits set to one
Example:
countSetBits(5) #returns 2 (0b0101), 2 bits set to 1
countSetBits(1) #returns 1 (0b0001), 1 bit set to 1
Returns the value of a bit or multiple bits of num.
Example:
num = 0b10001101
getBits(num, 0) # returns 1
getBits(num, 1) # returns 0
getBits(num, 4, 4) # returns 8 (0b1000)
getBits(num, 0, 8) # returns 141 (0b10001101)
Sets and returns the value of a bit or multiple bits of num.
Example:
num = 0b10001101 (141)
setBits(num, 0, 0) # returns 140 (0b10001100)
setBits(num, 1, 1) # returns 143 (0b10001111)
setBits(num, 4, 5, 4) # returns 221 (0b11011101)
Extends a number from bits bits to newBits bits
Example:
num = 0b0011
signExtend(num, 4, 10) # returns 0b0000000011
num = 0b1011
signExtend(num, 4, 10) # returns 0b1111111011
Returns signed value of num.
if msb equals zero, it returns num.
if msb equals one, it returns negative two’s complement of num.