How are signed integers represented using two’s complement?
- User
How are signed integers represented using two’s complement?
Assistant
Two's Complement: Representing Signed Integers
Two's complement is the most common method computers use to represent signed integers in binary. It elegantly handles both positive and negative numbers without needing a separate sign bit.
Core Concept
In an n-bit two's complement system:
- Positive numbers: Standard binary representation (most significant bit is 0)
- Negative numbers: Complement (flip) all bits of the positive number and add 1 (most significant bit is 1)
How to Find the Two's Complement of a Number
To represent -X in two's complement:
- Write X in binary
- Flip all bits (0→1, 1→0)
- Add 1 to the result
Key Properties
- The most significant bit (MSB) serves as the sign bit: 0 for positive, 1 for negative
- No wasted bit patterns (unlike sign-magnitude representation)
- Addition and subtraction work naturally without special handling
- The negative of a negative number is found by applying the same algorithm
Example (8-bit)
Decimal Binary (Two's Complement) Explanation +7 00000111 Standard binary -7 11111001 Flip bits of +7 (11111000) and add 1 +127 01111111 Largest positive number -128 10000000 Smallest negative number -1 11111111 Flip bits of +1 (00000001) and add 1 0 00000000 Zero (positive) Range for n-bit Systems
For n bits, the range is: -2^(n-1) to 2^(n-1)-1
- 8-bit: -128 to +127
- 16-bit: -32,768 to +32,767
- 32-bit: -2,147,483,648 to +2,147,483,647
Arithmetic Observation
In two's complement, adding a number and its negative yields all zeros (with an ignored overflow bit)—a natural consequence of the representation that makes arithmetic circuits simpler.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.