Free Online Utility • Binary Studio • Base-2 Engine

Swap Binary Bits

Swap specific bit positions (i ↔ j), adjacent bit pairs, or upper/lower halves inside binary values.

Formula & Quick Reference Base-2 Engine
Index: swap(B[i], B[j]) | Adjacent: (b0,b1)->(b1,b0)
Half (Nibble swap): 1100_1010 -> 1010_1100
Swap 0↔7 on 1000000000000001
Adjacent on 10100101
Half Swap on 1100101010101100
BIT POSITION SWAP
Swapped Binary Output

        
100% client-side exact Binary Engine Instant

Session Calculation Log

No entries recorded in this session yet.

How Swap Binary Bits Works

Swap Binary Bits exchanges any two bit positions (i ↔ j), swaps every adjacent even/odd bit pair, or swaps the upper and lower halves (nibble swap) of binary words.

Binary arithmetic and base-2 encoding form the physical foundation of digital microprocessors, network packets, memory buffers, and cryptographic primitives. Every calculation in this utility runs directly inside your browser using exact JavaScript BigInt and typed-array pipelines, so arbitrary-length bitstreams, 128-bit addresses, and raw byte buffers never suffer from 53-bit IEEE floating-point truncation or server latency.

Bitwise Exchange Operations

Adjacent Pair Swap: ((x & 0xAA) >> 1) | ((x & 0x55) << 1)
Nibble Swap (8-bit): ((x & 0x0F) << 4) | ((x & 0xF0) >> 4)

Swapping adjacent bits or nibbles is a classic bit-twiddling primitive used in hardware SWAP instructions, Morton Z-order de-interleaving, and cryptographic S-boxes.

Worked Reference Examples

InputOutputStep / Bit Note
11001010 (Half swap)10101100Upper & lower 4-bit nibbles swapped
10101010 (Adjacent)01010101Every adjacent pair exchanged
10000000 (0 ↔ 7)00000001MSB and LSB swapped

Practical Engineering Applications

Microcontroller Nibble Swapping

Verify PIC/AVR SWAPF nibble-exchange register instructions.

Bit-Twiddling & Interview Prep

Test bitwise XOR swap and adjacent-bit mask formulas interactively.

Precision, Edge Cases & Best Practices

Bit indices i and j are 0-based from the leftmost bit (MSB).

All input parsing strips harmless formatting whitespace, underscores, or common prefixes such as 0b where applicable, while preserving exact bit order and padding width so outputs drop straight into C/C++, Rust, Verilog, VHDL, Python, and embedded firmware test harnesses.

Frequently Asked Questions

What does Swap Adjacent Pairs do?
It swaps bit 0 with bit 1, bit 2 with bit 3, bit 4 with bit 5, and so on across the entire binary number.
How does Half Swap work on an 8-bit byte?
It swaps the upper 4-bit nibble with the lower 4-bit nibble (equivalent to the AVR/PIC SWAP instruction).
Does swapping bits change the count of 1s?
No — bit swapping is a pure permutation, preserving exact Hamming weight.