-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create swap_all_odd_and_even_bits.py #10692
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f77074a
Create swap_all_odd_and_even_bits.py
NikhithaBandari afefd27
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a941570
Update swap_all_odd_and_even_bits.py
cclauss f7c1147
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4c8e3fd
6: 00000110 --> 9: 00001001
cclauss ccccc95
Update swap_all_odd_and_even_bits.py
cclauss 8038fde
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
def show_bits(before: int, after: int) -> str: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> print(show_bits(0, 0xFFFF)) | ||
0: 00000000 | ||
65535: 1111111111111111 | ||
""" | ||
return f"{before:>5}: {before:08b}\n{after:>5}: {after:08b}" | ||
|
||
|
||
def swap_odd_even_bits(num: int) -> int: | ||
""" | ||
1. We use bitwise AND operations to separate the even bits (0, 2, 4, 6, etc.) and | ||
odd bits (1, 3, 5, 7, etc.) in the input number. | ||
2. We then right-shift the even bits by 1 position and left-shift the odd bits by | ||
1 position to swap them. | ||
3. Finally, we combine the swapped even and odd bits using a bitwise OR operation | ||
to obtain the final result. | ||
>>> print(show_bits(0, swap_odd_even_bits(0))) | ||
0: 00000000 | ||
0: 00000000 | ||
>>> print(show_bits(1, swap_odd_even_bits(1))) | ||
1: 00000001 | ||
2: 00000010 | ||
>>> print(show_bits(2, swap_odd_even_bits(2))) | ||
2: 00000010 | ||
1: 00000001 | ||
>>> print(show_bits(3, swap_odd_even_bits(3))) | ||
3: 00000011 | ||
3: 00000011 | ||
>>> print(show_bits(4, swap_odd_even_bits(4))) | ||
4: 00000100 | ||
8: 00001000 | ||
>>> print(show_bits(5, swap_odd_even_bits(5))) | ||
5: 00000101 | ||
10: 00001010 | ||
>>> print(show_bits(6, swap_odd_even_bits(6))) | ||
6: 00000110 | ||
9: 00001001 | ||
>>> print(show_bits(23, swap_odd_even_bits(23))) | ||
23: 00010111 | ||
43: 00101011 | ||
""" | ||
# Get all even bits - 0xAAAAAAAA is a 32-bit number with all even bits set to 1 | ||
even_bits = num & 0xAAAAAAAA | ||
|
||
# Get all odd bits - 0x55555555 is a 32-bit number with all odd bits set to 1 | ||
odd_bits = num & 0x55555555 | ||
|
||
# Right shift even bits and left shift odd bits and swap them | ||
return even_bits >> 1 | odd_bits << 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
for i in (-1, 0, 1, 2, 3, 4, 23, 24): | ||
print(show_bits(i, swap_odd_even_bits(i)), "\n") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.