TutorChase logo
Login
AQA A-Level Computer Science

5.4.3 Signed binary and two’s complement

Signed binary numbers use two’s complement to represent both positive and negative integers efficiently in binary, allowing for simpler arithmetic operations and consistent bit patterns.

What is signed binary?

In computer systems, data is often stored and manipulated in binary form. While unsigned binary is limited to non-negative integers such as 0, 1, 2, and so on, signed binary expands this to include negative numbers. This is crucial for performing a wider range of mathematical operations and enabling algorithms that rely on values less than zero.

Signed binary provides a way of indicating whether a binary number is positive or negative. There are a few methods for representing signed binary numbers:

  • Sign-and-magnitude

  • One’s complement

  • Two’s complement

Take your grades to the next level!

UPGRADING TO PREMIUM UNLOCKS
AI Tutor
AI-powered study assistant
instant feedback and guidance
Predicted Papers
Examiner-style predicted papers
based on recent exam trends
Practice Questions
All exam practice questions
by topic for each subject
Study Notes
All detailed revision notes
written by expert teachers
Cheat Sheets
Quick revision summaries
perfect for last-minute review
Past Papers
Complete collection
of practice and past exam papers
Email
Password
Confirm Password
Already have an account?

Practice Questions

FAQ

In two’s complement, the range of values for n bits is from −(2^(n−1)) to (2^(n−1))−1. This means there is one more negative number than positive numbers that can be represented. For example, with 8 bits, the range is −128 to +127. This asymmetry occurs because zero is assigned to the positive range, and the bit pattern for the most negative number (like 10000000 for −128) does not have a corresponding positive counterpart within the same bit width. This is due to how the MSB, which acts as the sign indicator, contributes to the overall value with a weight of −(2^(n−1)). The implication is that you must be cautious when trying to take the absolute value of the most negative number in programming or logic design—doing so will cause an overflow because its positive counterpart is not representable. Some processors throw an exception or wrap around in this case, depending on the system.

Overflow in two’s complement occurs when the result of an operation lies outside the representable range for a given bit width. This is particularly important in fixed-size binary systems where results that exceed this range will wrap around, leading to incorrect values. In addition to carry-out detection, processors use overflow flags that monitor the signs of the operands and the result. If two positive numbers are added and the result appears negative (MSB becomes 1), overflow has occurred. Similarly, if two negative numbers are added and the result is positive (MSB becomes 0), overflow is also detected. These flags are stored in the status register and are essential for conditional branching and program control. Detecting overflow is vital because undetected errors can lead to serious bugs in software, particularly in financial, control, or safety-critical systems. Programmers may need to write specific checks or rely on compiler intrinsics to safely handle overflow conditions.

Yes, two’s complement can be extended to any number of bits. Modern processors often use 32-bit or 64-bit two’s complement representations, significantly increasing the range of representable values. For n bits, the formula for the range remains the same: from −(2^(n−1)) to (2^(n−1))−1. This means a 32-bit system can represent integers from −2,147,483,648 to +2,147,483,647. Extending bit length doesn’t change the method of encoding or decoding the values—it only increases precision and expands the available range. Additionally, when extending bit width, values must be sign-extended properly. This involves replicating the MSB (the sign bit) into the additional higher-order bits to preserve the original number’s sign and value. For example, converting an 8-bit value 11101110 to 16-bit involves extending it to 1111111111101110, not simply prefixing with zeroes. Failure to perform sign extension accurately leads to incorrect values and is a common source of bugs in assembly and low-level programming.

Yes, negating a number in two’s complement twice will return the original value. This is because the two’s complement of a number is found by inverting all bits and then adding one. If you apply the same operation again, you return to the original binary representation. For instance, let’s take −12 in 8-bit two’s complement: starting with +12 = 00001100, invert to 11110011, then add 1 = 11110100 (which is −12). Negating −12 again: invert 11110100 = 00001011, add 1 = 00001100, which is +12. This works for all values except the most negative number in the range. For 8-bit numbers, this is −128 (10000000). If you try to negate −128, you attempt to represent +128, which is not representable in 8-bit two’s complement. This causes an overflow, and the result remains −128 due to bit wrapping. Therefore, while double negation generally returns the original number, this edge case must be handled carefully in system design.

Most programming languages and processors use two’s complement representation for signed integers by default, because it aligns well with binary arithmetic logic and simplifies hardware design. When writing code in languages like Python, Java, C++, or C#, you typically don’t see the binary representation directly—these languages abstract the details. However, when performing low-level operations (like bitwise shifting, masking, or working with raw memory), understanding two’s complement becomes crucial. For instance, right-shifting a negative number often retains the sign bit (known as arithmetic shift), while unsigned types shift in zeroes (logical shift). Additionally, in languages like C, integer overflow is undefined behaviour for signed integers, meaning the result is not guaranteed and can vary by compiler or architecture. In high-level code, issues may arise during type conversions, data truncation, or when working with external binary files. Being aware of how two’s complement works helps prevent bugs and ensures you interpret values correctly, especially when interfacing with low-level systems or hardware.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email