Skip to main content

shuffle-binary-bits

Shuffling binary bits randomly permutes the bit positions of a number β€” for example, 10110100 might become 01101100, with the same count of 1s but in different positions. Useful for cryptography demos (showing how diffusion works), constructing test vectors, and bit-permutation puzzles. The ZTools Shuffle Binary Bits tool uses Fisher-Yates on the bit positions (not the bit values) and supports both Math.random and crypto.getRandomValues for the shuffle.

Use cases​

Demonstrate diffusion in cryptography​

Many ciphers permute bits as part of confusion / diffusion. Random shuffle illustrates how a byte's positions can be rearranged.

Generate test vectors with same popcount​

Need 10 different 8-bit values each with exactly 4 ones. Shuffle a fixed pattern multiple times.

Bit-puzzle creation​

Show students a number, ask them to derive the shuffled version. Tests bit-position fluency.

Create scrambled-but-equal datasets​

For testing algorithms invariant to bit order (e.g. popcount-only algorithms), shuffled inputs verify the invariance.

How it works​

  1. Paste number β€” Binary / hex / decimal.
  2. Pick width β€” Determines which positions can be permuted.
  3. Pick RNG β€” Math.random (deterministic if seeded) or crypto.getRandomValues (truly random).
  4. Shuffle β€” Fisher-Yates on the bit array. Output: same popcount, different bit pattern.

Examples​

Input: 0b10110100 (8-bit)

Output: One shuffle: 0b01011010 (still 4 ones). Another: 0b11001010. Each permutation equally likely.


Input: 0xFF (8-bit)

Output: Always 0xFF β€” all positions identical.


Input: 0xF0 (8-bit) shuffled 5 times

Output: 5 different 8-bit values each with 4 ones β€” chosen uniformly from C(8,4) = 70 possible values.

Frequently asked questions​

Is the shuffle uniform?

Fisher-Yates produces uniform permutations of bit positions. Two values with the same popcount are equally likely as outputs.

Why preserve popcount?

Shuffling preserves bit-count by definition β€” only positions move. Useful invariant for crypto / coding-theory demos.

Reproducible?

Yes with Math.random + seed; no with crypto. Pick based on whether you need deterministic test cases.

Privacy?

All in browser.

Tips​

  • For crypto demos, use crypto RNG. For tests / homework / reproducibility, seeded Math.random.
  • Generate multiple shuffles of the same input to populate a test set with constant popcount.
  • Pair with the count-binary-ones tool to verify shuffle preserves popcount (invariant check).

Try it now​

The full shuffle-binary-bits runs in your browser at https://ztools.zaions.com/shuffle-binary-bits β€” no signup, no upload, no data leaves your device.

Open the tool β†—


Last updated: 2026-05-06 Β· Author: Ahsan Mahmood Β· Edit this page on GitHub