Skip to content

add reverse_bits.py #4120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 27, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions bit_manipulation/reverse_bits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
def get_reverse_bit_string(bn : int) -> str:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file bit_manipulation/reverse_bits.py, please provide doctest for the function get_reverse_bit_string

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file bit_manipulation/reverse_bits.py, please provide doctest for the function get_reverse_bit_string

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file bit_manipulation/reverse_bits.py, please provide doctest for the function get_reverse_bit_string

""" return the bit string of an interger
"""
bit_string = ""
for trk in range(0, 32):
bit_string += str(bn % 2)
bn = bn >> 1
return bit_string


def reverse_bit(n: int) -> str:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

"""
Take in an 32 bit integer, reverse its bits,
return a string of reverse bits

result of a reverse_bit and operation on the integer provided.

>>> reverse_bit(25)
'00000000000000000000000000011001'
>>> reverse_bit(37)
'00000000000000000000000000100101'
>>> reverse_bit(21)
'00000000000000000000000000010101'
>>> reverse_bit(58)
'00000000000000000000000000111010'
>>> reverse_bit(0)
'00000000000000000000000000000000'
>>> reverse_bit(256)
'00000000000000000000000100000000'
>>> reverse_bit(-1)
Traceback (most recent call last):
...
ValueError: the value of input must be positive

>>> reverse_bit(1.1)
Traceback (most recent call last):
...
TypeError: Input value must be a 'int' type

>>> reverse_bit("0")
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
"""
if n < 0:
raise ValueError("the value of input must be positive")
elif isinstance(n, float):
raise TypeError("Input value must be a 'int' type")
elif isinstance(n, str):
raise TypeError("'<' not supported between instances of 'str' and 'int'")
ans = 0
# iterator over [1 to 32],since we are dealing with 32 bit integer
for i in range(1, 33):
# left shift the bits by unity
ans = ans << 1
# get the end bit
k = n % 2
# right shif the bits by unity
n = n >> 1
# add that bit to our ans
ans = ans | k
return get_reverse_bit_string(ans)


if __name__ == "__main__":
import doctest
doctest.testmod()