Skip to content

Instantly share code, notes, and snippets.

@marmakoide
Created May 14, 2025 15:20
Show Gist options
  • Save marmakoide/027e92bb011b7c8704d57b8c548b874e to your computer and use it in GitHub Desktop.
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
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