Example #1
0
def num_in_numbits(num, numbits):
    """Does the integer `num` appear in `numbits`?

    Returns:
        A bool, True if `num` is a member of `numbits`.
    """
    nbyte, nbit = divmod(num, 8)
    if nbyte >= len(numbits):
        return False
    return bool(byte_to_int(numbits[nbyte]) & (1 << nbit))
Example #2
0
def num_in_numbits(num, numbits):
    """Does the integer `num` appear in `numbits`?

    Arguments:
        num (integer)

        numbits (binary blob)

    Returns:
        A boolean, true if `num` is a member of `numbits`.
    """
    nbyte, nbit = divmod(num, 8)
    if nbyte >= len(numbits):
        return False
    return bool(byte_to_int(numbits[nbyte]) & (1 << nbit))
Example #3
0
 def __getitem__(self, i):
     return byte_to_int(self.code[i])
Example #4
0
def good_numbits(numbits):
    """Assert that numbits is good."""
    # It shouldn't end with a zero byte, that should have been trimmed off.
    assert (not numbits) or (byte_to_int(numbits[-1]) != 0)
Example #5
0
 def test_binary_bytes(self):
     byte_values = [0, 255, 17, 23, 42, 57]
     bb = binary_bytes(byte_values)
     self.assertEqual(len(bb), len(byte_values))
     self.assertEqual(byte_values, list(bytes_to_ints(bb)))
     self.assertEqual(byte_values, [byte_to_int(b) for b in bb])
Example #6
0
 def __getitem__(self, i):
     return byte_to_int(self.code[i])