LearnViz CompSci
Home / Learn / Computer Architecture & Logic / Two's Complement

Beginner Topic 3 of 51 Computer Architecture & Logic

Two's Complement

Standard encoding of signed integers in binary.

How it works

Two's complement represents signed integers by letting the most significant bit carry a negative weight. To negate a number, invert all bits and add one. With n bits the range is minus 2 to the power n minus 1 up to 2 to the power n minus 1 minus 1. Addition and subtraction use the same hardware for signed and unsigned values, and there is only one zero.

Where it is used

Nearly every modern CPU and programming language stores signed integers this way. It explains overflow wraparound, sign extension when widening values and why bitwise negation works as it does.

Common pitfall

The range is asymmetric, so negating the most negative value overflows to itself. Signed overflow is undefined behavior in C. Comparing signed and unsigned values gives surprising results, and right shifts of negative numbers may be arithmetic or logical depending on the language. Always sign-extend, not zero-extend, when widening signed data.

Where it fits in the course

Two's Complement is topic 3 of 51 in Computer Architecture & Logic, rated Beginner. It follows Hexadecimal and Octal. The next topic is Floating Point Representation.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Computer Architecture & Logic quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Two's Complement?
Standard encoding of signed integers in binary. It belongs to Computer Architecture & Logic and is a beginner-level topic.
Where do people use Two's Complement?
Nearly every modern CPU and programming language stores signed integers this way.
What is the most common mistake with Two's Complement?
The range is asymmetric, so negating the most negative value overflows to itself.
What should I learn before this topic?
Start with Hexadecimal and Octal, the topic just before it in Computer Architecture & Logic.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Computer Architecture & Logic quiz. You can also explore Binary & Two's Complement in the Visual Lab.
Is Two's Complement hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn Two's Complement?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Two's Complement?
Hexadecimal and Octal, Floating Point Representation, Binary Number System are closely connected. The full list is in the related topics section of this page.
What should I study after Two's Complement?
Continue with Floating Point Representation, the next topic in Computer Architecture & Logic.

Related topics