-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Most important object in module is Binary
Class that represent numbers in binary. Wraps arithmetic to bouds of the binary number, allows for quick and easy bit manipulation.
- object - Any object that can be somehow converted to binary number. Including its representation as string, int, boolean, list of boolean-convertable values, byte-arrays, numpy arrays ect.
- bit_lenght - Target lenght of the number in bits. This number can be inferred from object based on its value, extra zeros ect
- bytes_lenght - Target lenght of the number in bytes. Same as bit_lenght but in bytes.
- sign_behavior - How number should implement sign.
- signed - if passed, will set sign_behavior to 'signed'
- default_formatting - formatting that will be applied if no other formating was passed
>>> Binary("0110") # From string representing binary number. Following zeros are used to inherit len of number.
'0110'
>>> Binary(4, bit_lenght=8) # From int and with overrided lenght
'00000100'
>>> Binary(255)
'11111111'
>>> Binary([True, 0, 1, 1.0]) # From array of boolean-likes
'1011'
>>> Binary("0000 0001") # Ignores whitespace
'00000001'
>>> Binary("ff Aa C C") # Works with hex too
'11111111 10101010 11001100'Module defines classes with predefined sizes and behaviors like u8, u16, i16, i64 ect. These ones can be used as followed:
>>> u8(3)
'00000011'Some conversions are avalible:
>>> num = Binary("FA") # 11111010To string
>>> str(num)
'11111010'
>>> num.to_string() # ignores formatting
'11111010'
>>> num.as_hex()
'fa'To int conversions to floats have almost no cost (reinterpreting array of bytes)
>>> int(num)
250To float
>>> float(num)
250.0To boolean (False when 0)
>>> bool(num)
TrueBits of the number can be accessed throught index:
>>> num = Binary("FA") # 11111010
>>> num[0], num[1], num[2] # First 3 bits of the number
(False, True, False)
>>> num[-1] # Last bit
True>>> num[:3] # First 3 bits
'010'
>>> num[-6:] # Last 6 bits
'111110'
>>> num[-6:-2] # From 6th bit from end to 2th bit from end
'1110'
>>> num[2:] # Skip first 2 bits
'111110'NOTE that behavior of slicing is slighty diffrent from slicing pythons str or list, first bit is from far right, not left.
- high_byte
- low_byte
- extended_low
- extended_high
- get_byte
- sign_behavior
- maximal_value
- minimal_value
- leading_zeros
- trailing_zeros
- is_negative
- append_high
- append_low
- strip
- strip_right
You can modify bits with indexes:
>>> num = Binary(0, bit_lenght=8)
>>> num[0] = True # Set first bit to high
>>> num[1] = True # Set second bit to high
>>> num
'00000011'
>>> num[-1] = True # Set last bit to high
>>> num
'10000011'
>>> num[0] = 0 # Set first bit to low
>>> num
'10000010'You can select bits by slice and set them, or copy from other number
>>> num = Binary(0, bit_lenght=8)
>>> num[:3] = True
>>> num
'00000111'
>>> num[:3] = "101" # Insert 101 in place of first 3 bits of the number
>>> num
'00000101'
>>> num[5:8] = "111" # insert 111 in place of bits 5th 6th 7th
>>> num
'11100101'
>>> num[:] = 0 # Set all bits to 0
>>> num
'00000000'There is few rules
- If right side is bool-like (so, bool or integer equal to 0 or 1), the assigment will set all selectet bits to this value.
- If right side is object convertable to Binary,
assigment will set selected slice only if lenght of the slice and lenght of the value
are equal. Assigment
num[:3] = "11"will fail. - step value is not supported.
You can select which bits should be changed with array.
>>> num = Binary(0, bit_lenght=8)
>>> num[[1,2,5]] = True # Set second, 3th and 5th bit to True
>>> num
'00100110'