Skip to main content

invert-binary-bits

Inverting binary bits flips every 0 to 1 and every 1 to 0 β€” the bitwise NOT (~) operation, also called one's complement. Result depends on bit width: ~0 is 1 in 1-bit width, 11111111 (255) in 8-bit, etc. The ZTools Invert Binary Bits tool inverts numbers given in binary, hex, or decimal at a chosen width, and shows before / after / decimal interpretation. Useful for bitmask construction, two's-complement learning, and verifying bit-operation code.

Use cases​

Build a bitmask for clearing bits​

Want to clear bits 2, 3, 5 of a value: build mask 0b00101100, NOT it (0b11010011), AND with the value.

Learn two's complement​

Two's complement of n = NOT(n) + 1 (in fixed width). Tool shows the NOT step.

Verify bitwise NOT in code​

C / Rust / Python bit-not vs JavaScript ~ operator differ subtly. Tool shows the unambiguous result.

Generate "all-bits-set" patterns​

NOT(0) = all 1s in chosen width. Useful for mask construction.

How it works​

  1. Paste number β€” Binary / hex / decimal.
  2. Pick width β€” 8 / 16 / 32 / 64 bits. Width determines the result's magnitude.
  3. Invert β€” XOR with all-1s mask of chosen width. Display result in same notation.
  4. Output β€” Inverted number in binary / hex / decimal. Plus signed two's-complement interpretation.

Examples​

Input: Decimal 5, width 8

Output: Binary 00000101 β†’ 11111010. Decimal 250 (unsigned) or βˆ’6 (signed two's complement).


Input: Hex 0xF0, width 8

Output: 0x0F. Easy to see.


Input: 0xFFFFFFFF, width 32

Output: 0x00000000. NOT of all-1s is all-0s, regardless of width.

Frequently asked questions​

Why does the result depend on width?

NOT(5) is 1...11010 β€” but how many leading 1s depends on width. In 8-bit, 250 (or βˆ’6 signed). In 16-bit, 65530 (or βˆ’6 signed). In 32-bit, 4294967290 (or βˆ’6 signed). Always specify width.

JavaScript ~5 = -6 β€” why?

JavaScript uses 32-bit signed integers for bitwise operations. ~5 is 0xFFFFFFFA, interpreted as signed = βˆ’6. Tool clarifies which width is used.

One's vs two's complement?

One's complement = NOT (just flip all bits). Two's complement = NOT + 1 (gives a clean negate). Modern computers use two's complement for negative numbers.

Privacy?

All in browser.

Tips​

  • Always pick the width matching your target system (Java int = 32-bit, long = 64-bit, etc.).
  • For mask construction, use NOT(small mask) to get an inverted-mask in chosen width.
  • For two's complement, NOT + 1 β€” the tool can show both steps.

Try it now​

The full invert-binary-bits runs in your browser at https://ztools.zaions.com/invert-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