Java Syntax · Lesson 6/9
67%
⏱ 5 min read Modified: 2026-07-04

Sign-Magnitude, One's Complement and Two's Complement

How integer numbers are stored in computer memory — and how the three binary encoding schemes differ.

1. How a byte looks in memory

Numbers in computer memory are stored in binary form. The byte type in Java is 8 bits wide. Each bit holds either 0 or 1:

0
0
0
1
0
1
0
1
sign
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0

In signed integers, the leftmost (most significant) bit is the sign bit: 0 means positive, 1 means negative. The remaining 7 bits encode the magnitude. This foundation is shared by all three encoding schemes.

2. Sign-magnitude

The most intuitive encoding. The sign bit indicates direction, and the remaining 7 bits store the exact binary magnitude of the number.

Decimal Sign-magnitude (8 bits)
3 0 000 0011
2 0 000 0010
1 0 000 0001
0 0 000 0000
−0 1 000 0000
−1 1 000 0001
−2 1 000 0010
Limitation: Sign-magnitude produces two representations of zero — 0 000 0000 (+0) and 1 000 0000 (−0). Arithmetic circuits must handle this as a special case, which makes hardware more complex. Modern CPUs use two's complement instead.

3. One's complement

To negate a number in one's complement, you invert every single bit — including the sign bit. No bit is excluded.

+4 (original)
0
0
0
0
0
1
0
0
−4 (one's complement)
1
1
1
1
1
0
1
1

One's complement still has the two-zeros problem. 1 111 1111 represents −0, distinct from +0 (0 000 0000). Two's complement solves this.

4. Two's complement

This is how Java (and virtually every modern CPU) stores negative integers. It eliminates double-zero and makes addition and subtraction the same hardware operation regardless of sign.

Converting a positive number to its two's complement negative

1

Invert all bits — including the sign bit (this gives the one's complement):

+4
0
0
0
0
0
1
0
0
one's complement
1
1
1
1
1
0
1
1
2

Add 1 to the result:

one's complement
1
1
1
1
1
0
1
1
+ 1

two's complement (−4)
1
1
1
1
1
1
0
0
Verification: The two's complement of −4 is 1111 1100. Check: 1111 1100 + 0000 0100 = 0000 0000 (overflow discarded) ✓

5. Full comparison table

Decimal Sign-magnitude One's complement Two's complement
0 0 000 0000 0 000 0000 0 000 0000
−0 1 000 0000 1 111 1111 — (no −0)
−1 1 000 0001 1 111 1110 1 111 1111
−2 1 000 0010 1 111 1101 1 111 1110
−3 1 000 0011 1 111 1100 1 111 1101
−4 1 000 0100 1 111 1011 1 111 1100
−5 1 000 0101 1 111 1010 1 111 1011
Key insight: In two's complement, zero has exactly one representation. This frees up a bit pattern for an extra negative value — an 8-bit signed integer covers −128 to +127, not the symmetric −127 to +127.

6. Integer ranges in Java

All four Java integer primitives use two's complement. The asymmetry (one extra negative) is a direct consequence of eliminating −0.

byte · 8 bits
−128
to
+127
short · 16 bits
−32 768
to
+32 767
int · 32 bits
−2 147 483 648
to
+2 147 483 647
long · 64 bits
−9 223 372 036 854 775 808
to
+9 223 372 036 854 775 807

Frequently Asked Questions

Why is the byte range −128 to +127 instead of symmetric?

Because Java stores numbers in two's complement, where zero has only one representation. In sign-magnitude and one's complement, zero takes two bit patterns (+0 and −0), while in two's complement it takes one. The freed-up pattern is used for an extra negative value. That's why an 8-bit signed integer covers −128 to +127: there's one more negative value than positive.

Why is two's complement needed if sign-magnitude exists?

Two's complement solves two problems of sign-magnitude. First, zero gets a single representation (no separate +0 and −0). Second, addition and subtraction of numbers of any sign use the same hardware operation — the CPU doesn't need to handle the sign separately. This simplifies and speeds up computation, which is why virtually all modern CPUs and Java use two's complement.

How does one's complement differ from two's complement?

One's complement is obtained by inverting all bits of a number. Two's complement is the one's complement plus one. The key difference is the result: one's complement still has two representations of zero (the −0 problem remains), while two's complement has only one. That's why modern systems use two's complement rather than one's complement.

What is the sign bit?

The sign bit is the most significant (leftmost) bit in a signed integer. It indicates the sign: 0 means positive, 1 means negative. The remaining bits encode the magnitude of the number. For example, in an 8-bit byte, 1 bit is used for the sign and the remaining 7 bits for the magnitude. The sign bit is the foundation of all three encoding schemes: sign-magnitude, one's complement, and two's complement.

Comments

Please log in or register to have a possibility to add comment.

‹ Previous lesson Next lesson ›