def sign_ext(self, new_bit_count: int) -> 'Bits': value = self.value if self.get_sign_bit() and new_bit_count >= self.bit_count: value |= bit_helpers.to_mask(new_bit_count - self.bit_count) << self.bit_count return Bits(bit_count=new_bit_count, value=value & bit_helpers.to_mask(new_bit_count))
def zero_ext() -> Value: assert isinstance(type_, BitsType) constructor = Value.make_sbits if type_.signed else Value.make_ubits bit_count = type_.get_total_bit_count() return constructor( bit_count, value.get_bits_value() & bit_helpers.to_mask(bit_count))
def do_shra(lhs: int, rhs: int): if (lhs >> (self.bit_count - 1)) & 1: if rhs >= self.bit_count: return self.get_mask() return (lhs >> rhs) | ( bit_helpers.to_mask(rhs) << (self.bit_count - rhs)) else: if rhs >= self.bit_count: return 0 return lhs >> rhs
def make_bit_patterns(bit_count: int) -> Tuple[int, ...]: """Creates a sequence of interesting bit patterns at a given bit count.""" if bit_count == 0: return (0,) all_ones = int('0b' + '1' * bit_count, 2) all_but_high_ones = int('0b0' + '1' * (bit_count - 1), 2) off_on = int( '0b' + '01' * (bit_count // 2) + ('0' if bit_count % 2 != 0 else ''), 2) on_off = int( '0b' + '10' * (bit_count // 2) + ('1' if bit_count % 2 != 0 else ''), 2) one_hots = tuple( int('0b{}1{}'.format('0' * pos, '0' * (bit_count - pos - 1)), 2) for pos in range(bit_count)) zero = 0 result = (all_ones, all_but_high_ones, off_on, on_off, zero) + one_hots assert all(x & bit_helpers.to_mask(bit_count) == x for x in result) return result
def get_mask(self) -> int: return bit_helpers.to_mask(self.bit_count)
def zero_ext(self, new_bit_count: int) -> 'Bits': return Bits( bit_count=new_bit_count, value=self.value & bit_helpers.to_mask(new_bit_count))
def get_lsb_slice(self, count: int) -> 'Bits': return Bits(bit_count=count, value=self.value & bit_helpers.to_mask(count))