Bit Manipulation Toolkit

Set, clear, toggle or test one bit, rotate, reverse the bit order or pick out the lowest set bit. Every value also shows its popcount, leading and trailing zeros, at 8 to 64 bits.

Calculator Number Systems Updated Oct 3, 2026
How to Use
  1. Enter a value in decimal, hex (0x…) or binary (0b…) and choose the width.
  2. Pick an operation: set, clear, toggle or test a bit, rotate, reverse, or the lowest set bit.
  3. For bit operations enter the bit index, counting from 0 at the right; for rotations, the number of places.
  4. Read the result, and the popcount and leading and trailing zeros of your value.
  5. The drawing shows the bits before and after, with the bit you touched outlined.
Input
0 = rightmost
Presets
Before and after
Result
—
Result (decimal)
—
Set bits in x
—
Leading / trailing zeros
—

Worked Example

Working on 0x2C = 0010 1100. To set bit 0, OR with 1 << 0 = 0000 0001: 0010 1101 = 0x2D. To clear bit 3, AND with ~(1 << 3) = 1111 0111: 0010 0100 = 0x24. To test bit 2, shift right 2 and keep the last bit: 0000 1011 & 1 = 1. The value has 3 set bits, 2 leading zeros and 2 trailing zeros.

Rotating 0x81 = 1000 0001 left by one. Every bit moves up one place and the top bit wraps round to the bottom: 0000 0011 = 0x03. Rotating right instead gives 1100 0000 = 0xC0.

The common mistake: an off-by-one bit index. Bits count from 0 at the right, so the “third bit” is bit 2, worth 4, not bit 3, worth 8. And in C, 1 << 31 overflows a signed int; write 1u << 31 or 1ULL << k for wide masks.

Show Work

Enter a value to see the step-by-step working.

Formulas

Set · clear · toggle
x | (1<<k) · x & ~(1<<k) · x ^ (1<<k)
Test
(x >> k) & 1
Rotate left
(x << s) | (x >> (w − s))
masked to w bits
Lowest set bit
x & −x
x & (x − 1) clears it
Highest bit
⌊log₂ x⌋ = w − 1 − clz(x)

Bit Twiddling

Programmers have collected tricks for single bits since the earliest machines. MIT’s HAKMEM memo of 1972 gathered dozens, including a famous way to count bits with a few masks and shifts, and Henry Warren’s book Hacker’s Delight (2002) became the standard reference: x & (x − 1) clears the lowest set bit, x & −x keeps only it, and the number of times you can do the first before reaching zero is the popcount.

Many of these now have their own instructions. x86 added POPCNT, LZCNT and TZCNT, ARM has CLZ, RBIT and CNT, and C++20 exposes them as std::popcount, std::countl_zero, std::rotl and friends, so the compiler emits one instruction instead of a loop.

About This Calculator

This toolkit sets, clears, toggles and tests a single bit, rotates left or right, reverses the bit order and isolates the lowest set bit, at 8, 16, 32 or 64 bits, and always shows the popcount, the leading and trailing zeros and ⌊log₂ x⌋. It draws the bits before and after with the bit you changed outlined.

Everything runs in your browser; nothing is sent anywhere. For AND, OR, XOR and shifts between two numbers, use the bitwise calculator.

Related calculators: Bitwise Calculator, Bit Field Designer, and Endianness Converter.

Frequently Asked Questions

How do I set, clear or toggle a bit?

Make a mask with a 1 in the bit you want, 1 << k. OR it in to set the bit (x | mask), AND with its inverse to clear it (x & ~mask), and XOR to flip it (x ^ mask). To test a bit, shift it down and keep the last bit: (x >> k) & 1.

How are bits numbered?

From 0 at the right, the least significant bit, which is worth 1. Bit k is worth 2ᵏ, so bit 7 is the top bit of a byte, worth 128. Some hardware datasheets number from the left instead, so check before copying a bit number.

What is popcount?

The population count, or Hamming weight: the number of 1 bits. 0x2C = 0010 1100 has 3; 0xDEADBEEF has 24. Most CPUs count them in one instruction (POPCNT on x86, CNT on ARM), and it is used in error-correcting codes, chess engines and similarity hashes.

What is the difference between a rotate and a shift?

A shift drops the bits that fall off one end and fills the other end with zeros. A rotate wraps them round: rotating 1000 0001 left by one gives 0000 0011, because the top bit comes back in at the bottom. Rotations are used in hash functions and ciphers such as SHA-2 and ChaCha.

What do leading and trailing zeros tell me?

Leading zeros count the zeros above the highest 1, so ⌊log₂ x⌋ = width − 1 − leading zeros: 1,000 has 22 leading zeros in 32 bits, so its highest bit is bit 9 and 2⁹ ≤ 1,000 < 2¹⁰. Trailing zeros count how many times x divides by 2, and x & −x isolates that lowest 1.

How do I use the Bit Manipulation Toolkit?

Just type your numbers. The answer shows up right away — there is no button to press. Change anything and it updates by itself.

Is it free? Does it work without internet?

Yes to both. It is free with no sign-up, and once the page has loaded it keeps working even with no internet.

Where does my data go?

Nowhere — every calculation runs on your own device. Nothing you enter is uploaded, logged, or stored.

Common Use Cases

Flags

Set bit 0 of 0x2C to get 0x2D; clear bit 3 to get 0x24; test bit 2: it is 1.

Hashes and ciphers

Rotate 0x81 left by 1 to get 0x03, and right by 1 to get 0xC0.

Bit reversal

0x2C = 0010 1100 reversed is 0011 0100 = 0x34, as used in FFTs and LSB-first protocols.

Counting

0xDEADBEEF has 24 set bits; 1,000 has 22 leading zeros at 32 bits, so ⌊log₂ 1000⌋ = 9.

Last updated: