FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TheAlgorithms-Python/bit_manipulation/reverse_bits.py at master · windNight/TheAlgorithms-Python · GitHub
windNight
/
TheAlgorithms-Python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TheAlgorithms-Python
/
bit_manipulation
/
reverse_bits.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
85 lines (75 loc) · 2.52 KB
Breadcrumbs
TheAlgorithms-Python
/
bit_manipulation
/
reverse_bits.py
Copy path
File metadata and controls
85 lines (75 loc) · 2.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def
get_reverse_bit_string
(
number
:
int
)
->
str
:
"""
return the bit string of an integer
>>> get_reverse_bit_string(9)
'10010000000000000000000000000000'
>>> get_reverse_bit_string(43)
'11010100000000000000000000000000'
>>> get_reverse_bit_string(2873)
'10011100110100000000000000000000'
>>> get_reverse_bit_string("this is not a number")
Traceback (most recent call last):
...
TypeError: operation can not be conducted on a object of type str
"""
if
not
isinstance
(
number
,
int
):
raise
TypeError
(
"operation can not be conducted on a object of type "
f"
{
type
(
number
).
__name__
}
"
)
bit_string
=
""
for
_
in
range
(
0
,
32
):
bit_string
+=
str
(
number
%
2
)
number
=
number
>>
1
return
bit_string
def
reverse_bit
(
number
:
int
)
->
str
:
"""
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
number
<
0
:
raise
ValueError
(
"the value of input must be positive"
)
elif
isinstance
(
number
,
float
):
raise
TypeError
(
"Input value must be a 'int' type"
)
elif
isinstance
(
number
,
str
):
raise
TypeError
(
"'<' not supported between instances of 'str' and 'int'"
)
result
=
0
# iterator over [1 to 32],since we are dealing with 32 bit integer
for
_
in
range
(
1
,
33
):
# left shift the bits by unity
result
=
result
<<
1
# get the end bit
end_bit
=
number
%
2
# right shift the bits by unity
number
=
number
>>
1
# add that bit to our ans
result
=
result
|
end_bit
return
get_reverse_bit_string
(
result
)
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
Back
|
FazBrowse Home
|
New Git URL