We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5f697c4 commit b48f8fdCopy full SHA for b48f8fd
snippets/python/reverse_int_bits.md
@@ -0,0 +1,24 @@
1
+# Reverse bits of an integer
2
+*tags:* **bit manipulation, python**
3
+
4
5
+**Snippet**
6
+```python
7
8
+def reverseBits(n):
9
+ # Initially all bits are zero
10
+ reverseInt = 0
11
+ # Size of int is 32 bits
12
+ for bitPos in range(32):
13
+ # Check if bit at position "bitPos" is 1,
14
+ # If that bit is 0, then we don't need to add
15
+ # it to reverseInt since it won't make a difference
16
+ if n & (1 << bitPos):
17
+ # Change bit at "bitPos" to 1
18
+ reverseInt = reverseInt | (1 << (31-bitPos))
19
20
+ return revAns
21
22
+print(reverseBits(10))
23
24
+```
0 commit comments