Two's Complement
Represents negative integers in binary by inverting bits and adding one.
Formula
Variables
Example
In 8-bit two’s complement, −5 is ~00000101 + 1 = 11111011.
How it works
In two’s complement arithmetic, a negative number is stored by taking the bitwise inverse of the positive magnitude and then adding 1. This works because, for a fixed bit-width, the result is the additive inverse modulo 2^n: when you add a number to this transformed version, the carry wraps around and produces 0 in the low n bits. The formula only makes sense relative to a chosen register size, since overflow beyond that width is intentionally discarded.
When to use it
Use this when converting signed integers to binary for CPUs, digital circuits, low-level debugging, or manually checking machine code behavior. It is especially important when reasoning about overflow, subtraction, or how signed values are represented in a fixed number of bits.
Worked example, step by step
Find the 8-bit two’s complement representation of −18.
Step 1: Write +18 in 8-bit binary: 00010010
Step 2: Invert every bit: 11101101
Step 3: Add 1: 11101101 + 1 = 11101110
So, −18 = 11101110 in 8-bit two’s complement.
Common mistakes to avoid
- Forgetting that the bit-width is fixed, so extra carry bits are discarded
- Inverting the bits without adding 1, which gives one’s complement instead
- Using the formula on a decimal number directly instead of first writing its binary form
Did You Know?
Two's complement lets computers subtract by adding — which is why almost every CPU uses it for signed integers.
Share this formula
More in Computer Science
View allGustafson’s Law
IntermediateIdeal scaled-workload speedup when more processors allow a larger parallel task in the same elapsed time. Here s is the serial fraction of the parallel execution time, not the single-processor fraction used in Amdahl’s law.
Hick–Hyman Law (Equal Choices)
IntermediateModels choice reaction time for n equally likely alternatives (n ≥ 1). This information-theoretic form assumes comparable, practised stimulus–response choices; it is not a universal law of menu browsing.
Fitts’s Law (Shannon Form)
IntermediateAn empirical model of aimed movement time: farther or narrower targets generally take longer to reach. Use D ≥ 0, W > 0 and fitted coefficients for the device and task.
Shannon Entropy
AdvancedAverage information content of a random source (bits).
Master Theorem (form)
AdvancedAnalyzes running time of divide-and-conquer algorithms.
Hamming Distance
AdvancedNumber of positions at which two strings differ.