Language: EN

que-es-signed-unsigned-en-binario

What is signed and unsigned in the binary system

In the binary system, integers can be represented either signed (with sign) or unsigned (without sign). The difference is that

  • Unsigned: Numbers are only positive
  • Signed: Numbers are positive or negative

This is something that is very confusing at first. But let’s remember that one thing is a number, and another thing is the representation of a number. So

11001000

Is the representation of a number in binary. The “real” number is

  • 200, if I consider it an 8bits unsigned
  • -56, if I consider it an 8bits signed

What a difference! But the issue is even worse. Because the length of the number also influences. If we move to 16 bits.

00000000 11001000

That number,

  • 200 if I consider it a 16bits unsigned
  • 200 if I consider it a 16bits signed

Why? Because I still have many zeros on the left to reach the negative numbers. For it to be -56 in 16 bits it would have to be

11111111 11001000

That number is

  • 32740 if I consider it a 16bits unsigned
  • -56 if I consider it a 16bits signed

Signed and unsigned

Signed

In the signed representation, the most significant bit (the leftmost bit) is used to indicate the sign of the number. A bit of 0 indicates a positive number, while a bit of 1 indicates a negative number.

For example, if we are working with a byte (8 bits), the range of signed integer numbers is from -128 to 127. The leftmost bit is the sign bit, so there are only 7 bits available to represent the absolute value of the number.

Unsigned

In the unsigned representation, all bits are used to represent the absolute value of the number. There is no dedicated sign bit, so all numbers are considered positive.

For example, in a byte (8 bits), the range of unsigned integer numbers is from 0 to 255. All bits are available to represent the absolute value of the number.

Ranges and Number of Bytes

As we said, the number of bytes and whether they are signed or unsigned directly impacts the range of values we can store and the number we are representing.

Here is a table summarizing the ranges and the number of bytes commonly used in the representation of integer numbers:

Signed / UnsignedNumber of BytesRange
Unsigned1 byte0 to 255
Unsigned2 bytes0 to 65,535
Unsigned4 bytes0 to 4,294,967,295
Signed1 byte-128 to 127
Signed2 bytes-32,768 to 32,767
Signed4 bytes-2,147,483,648 to 2,147,483,647

Extended Range for Negative Numbers

It is important to mention that the range of negative numbers often has a more negative value than positive for one reason, that the positives include zero.

In the signed representation, zero is included in the positive range, which means that the negative range needs to include an additional number to compensate.

For example, in one byte, the signed range is from -128 to 127, where -128 is a more negative number than -127. This is because zero is in the positive range.