Skip to main content

Ones Complement Calculator

Free Ones complement Calculator for exponents & logarithms. Enter values to get step-by-step solutions with formulas and graphs.

Share this calculator

Formula

Ones Complement = Flip all bits (0 to 1, 1 to 0)

The ones complement of a binary number is obtained by inverting every bit. For an n-bit number N, the ones complement equals (2^n - 1) - N. The original plus its ones complement always equals a string of all 1s (2^n - 1).

Worked Examples

Example 1: 8-Bit Ones Complement of 42

Problem: Find the ones complement of decimal 42 using 8-bit representation.

Solution: Step 1: Convert 42 to 8-bit binary\n42 = 00101010\n\nStep 2: Flip all bits (0 becomes 1, 1 becomes 0)\n00101010 -> 11010101\n\nStep 3: Verify\nOriginal + Complement = 00101010 + 11010101 = 11111111 (all ones)\n\nUnsigned decimal: 11010101 = 213\nSigned interpretation: -42

Result: Ones complement of 42 = 11010101 (unsigned 213, signed -42)

Example 2: 16-Bit Ones Complement for Checksum

Problem: Compute the ones complement of the 16-bit value 0xAB12 for an IP checksum calculation.

Solution: Step 1: Convert 0xAB12 to binary\n0xAB12 = 1010101100010010\n\nStep 2: Flip all bits\n1010101100010010 -> 0101010011101101\n\nStep 3: Convert back\n0101010011101101 = 0x54ED (decimal 21,741)\n\nVerify: 0xAB12 + 0x54ED = 0xFFFF (all ones)

Result: Ones complement of 0xAB12 = 0x54ED

Frequently Asked Questions

What is ones complement in binary?

Ones complement is a method of representing signed integers in binary by flipping (inverting) all the bits of a number. Every 0 becomes 1 and every 1 becomes 0. For example, the ones complement of 00101010 (42 in 8-bit binary) is 11010101 (213 as unsigned, or -42 in ones complement signed representation). This system uses the most significant bit (MSB) as a sign bit: 0 for positive numbers and 1 for negative numbers. Ones complement was used in early computers like the UNIVAC and CDC 6600, though it has been largely replaced by twos complement in modern systems due to its simpler arithmetic properties.

How does ones complement differ from twos complement?

The key differences are in how negative numbers are represented and how arithmetic works. Ones complement negates by flipping all bits, while twos complement flips all bits and then adds 1. Ones complement has two representations of zero: positive zero (00000000) and negative zero (11111111), whereas twos complement has only one zero. In ones complement arithmetic, end-around carry must be added back when overflow occurs, complicating addition circuits. Twos complement avoids this issue and provides one extra negative number in its range. For 8 bits, ones complement ranges from -127 to +127, while twos complement ranges from -128 to +127.

Why was ones complement used in early computers?

Ones complement was popular in early computing because negation is trivially simple: just invert every bit, which can be done with a single layer of NOT gates and requires no sequential logic. This made the hardware for negation very fast and cheap. Computers like the UNIVAC 1100 series and CDC 6600 used ones complement arithmetic. The trade-off was more complex addition logic (due to end-around carry) and the dual representation of zero. As transistor costs decreased and circuit design improved, the advantages of twos complement (simpler addition, single zero) outweighed the simplicity of ones complement negation, and virtually all modern processors use twos complement.

What is end-around carry in ones complement addition?

End-around carry is a technique required when adding ones complement numbers. If the addition of two n-bit numbers produces a carry out of the most significant bit position, that carry must be added back to the least significant bit of the result. For example, adding 01010 (10) and 11001 (-6 in ones complement) gives 1 00011 with a carry. Adding the carry back gives 00100 (4), which is correct since 10 + (-6) = 4. Without end-around carry, the result would be off by one. This extra step adds complexity to the arithmetic logic unit and is the primary reason modern computers prefer twos complement, which handles addition without this correction.

How is ones complement used in network protocols?

Ones complement arithmetic is still actively used in Internet protocol checksums, specifically in IPv4, TCP, UDP, and ICMP headers. The checksum is computed by taking the ones complement sum of 16-bit words in the header. The sender calculates this checksum and places it in the header. The receiver recomputes the checksum over the received data (including the checksum field) and verifies the result is all 1s (or equivalently, all 0s after taking the complement). This design was chosen because ones complement checksums are endian-independent and easy to update incrementally when only a few fields change, such as when a router decrements the TTL field.

What is the range of numbers representable in ones complement?

For an n-bit ones complement system, the range is -(2^(n-1) - 1) to +(2^(n-1) - 1). For 8 bits, this is -127 to +127 (255 distinct values plus the duplicate zero). For 16 bits, the range is -32,767 to +32,767. Compare this with twos complement where n bits give a range of -2^(n-1) to +(2^(n-1) - 1), providing one additional negative value. The reduced range in ones complement comes from the dual representation of zero, which wastes one bit pattern. For unsigned interpretation, the same n bits represent 0 to 2^n - 1. Understanding these ranges is crucial for preventing overflow errors in digital systems.

References