HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

What are two's complement integers?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
whataretwocomplementintegers

Problem

Can someone explain in plain English what "two's complement integer" means? I read this:


in Java long is a 64-bit signed two's complement integer

Solution

Two's complement is the most commonly used way to represent signed integers in bits. First, consider unsigned numbers in 8 bits. Notice that $2^8 = 256 = 100000000_2$ does not fit into 8 bits and will thus be represented as 0000 0000. Therefore $255 + 1 =$ 1111 1111 + 0000 0001 = 0000 0000 and in that sense 1111 1111 acts as if it was $-1$. Two's complement is based on this observation.

For a deeper understanding of it, I suggest reading the wikipedia page on it. Note that as long as you you don't need to directly manipulate bits of your integers and your numbers don't go out of range, it makes no difference what signed integer representation is used.

Context

StackExchange Computer Science Q#10958, answer score: 7

Revisions (0)

No revisions yet.