mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Added binery_or_operator.py to bit manipulation file (#2331)
* added bitwise binary OR operator * Rename binary_OR_operator.py to binary_or_operator.py * Update binary_or_operator.py * Update binary_or_operator.py * Update bit_manipulation/binary_or_operator.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update binary_or_operator.py * Update binary_or_operator.py * Nice!! Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
30126c26dd
commit
61dde44434
48
bit_manipulation/binary_or_operator.py
Normal file
48
bit_manipulation/binary_or_operator.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm
|
||||||
|
|
||||||
|
|
||||||
|
def binary_or(a: int, b: int):
|
||||||
|
"""
|
||||||
|
Take in 2 integers, convert them to binary, and return a binary number that is the
|
||||||
|
result of a binary or operation on the integers provided.
|
||||||
|
|
||||||
|
>>> binary_or(25, 32)
|
||||||
|
'0b111001'
|
||||||
|
>>> binary_or(37, 50)
|
||||||
|
'0b110111'
|
||||||
|
>>> binary_or(21, 30)
|
||||||
|
'0b11111'
|
||||||
|
>>> binary_or(58, 73)
|
||||||
|
'0b1111011'
|
||||||
|
>>> binary_or(0, 255)
|
||||||
|
'0b11111111'
|
||||||
|
>>> binary_or(0, 256)
|
||||||
|
'0b100000000'
|
||||||
|
>>> binary_or(0, -1)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: the value of both input must be positive
|
||||||
|
>>> binary_or(0, 1.1)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: 'float' object cannot be interpreted as an integer
|
||||||
|
>>> binary_or("0", "1")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: '<' not supported between instances of 'str' and 'int'
|
||||||
|
"""
|
||||||
|
if a < 0 or b < 0:
|
||||||
|
raise ValueError("the value of both input must be positive")
|
||||||
|
a_binary = str(bin(a))[2:] # remove the leading "0b"
|
||||||
|
b_binary = str(bin(b))[2:]
|
||||||
|
max_len = max(len(a_binary), len(b_binary))
|
||||||
|
return "0b" + "".join(
|
||||||
|
str(int("1" in (char_a, char_b)))
|
||||||
|
for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user