-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
add reverse_bits.py #4120
Changes from 3 commits
cd110ab
3e9f11e
dc4c3a7
7566e75
fba99a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
def get_reverse_bit_string(bn : int) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
""" 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
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() |
There was a problem hiding this comment.
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 functionget_reverse_bit_string