bit_manipulation.binary_and_operator¶
Bitwise AND helper.
Return a zero-padded binary string representing a & b where the width is the
maximum bit length of the inputs. Only non-negative integers are accepted.
>>> binary_and(25, 32)
'0b000000'
>>> binary_and(37, 50)
'0b100000'
>>> binary_and(21, 30)
'0b10100'
>>> binary_and(58, 73)
'0b0001000'
>>> binary_and(0, 255)
'0b00000000'
>>> binary_and(256, 256)
'0b100000000'
Invalid inputs raise clear exceptions:
>>> binary_and(0, -1)
Traceback (most recent call last):
...
ValueError: inputs must be non-negative integers
>>> binary_and(0, 1.1)
Traceback (most recent call last):
...
TypeError: inputs must be integers
>>> binary_and("0", "1")
Traceback (most recent call last):
...
TypeError: inputs must be integers
Functions¶
|
Take in 2 integers, convert them to binary, |
Module Contents¶
- bit_manipulation.binary_and_operator.binary_and(a: int, b: int) str¶
Take in 2 integers, convert them to binary, return a binary number that is the result of a binary and operation on the integers provided.
>>> binary_and(25, 32) '0b000000' >>> binary_and(37, 50) '0b100000' >>> binary_and(21, 30) '0b10100' >>> binary_and(58, 73) '0b0001000' >>> binary_and(0, 255) '0b00000000' >>> binary_and(256, 256) '0b100000000' >>> binary_and(0, -1) Traceback (most recent call last): ... ValueError: inputs must be non-negative integers >>> binary_and(0, 1.1) Traceback (most recent call last): ... TypeError: inputs must be integers >>> binary_and("0", "1") Traceback (most recent call last): ... TypeError: inputs must be integers >>> binary_and(10, "1") Traceback (most recent call last): ... TypeError: inputs must be integers