Created
May 14, 2025 15:20
-
-
Save marmakoide/027e92bb011b7c8704d57b8c548b874e to your computer and use it in GitHub Desktop.
Generates bit reversal sequences ie. a permutation of integer from 0 to N - 1
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
import math | |
def bit_reversal_sequence(N): | |
bit_count = int(math.ceil(math.log2(N))) | |
for i in range(2 ** bit_count): | |
bit_str = bin(i)[2:].rjust(bit_count, '0') | |
bit_str = ''.join(reversed(bit_str)) | |
j = int(bit_str, 2) | |
if j < N: | |
yield j |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment