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 as signed or unsigned. The difference is that

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

This is something that confuses a lot at first. But remember, 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 as an 8-bit unsigned
  • -56, if I consider it as an 8-bit signed

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

00000000 11001000

That number,

  • 200 if I consider it as a 16-bit unsigned
  • 200 if I consider it as a 16-bit signed

Why? Because I still have many zeros on the left to reach 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 as a 16-bit unsigned
  • -56 if I consider it as a 16-bit 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 integers 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 bit dedicated to the sign, so all numbers are considered positive.

For example, in a byte (8 bits), the range of unsigned integers 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 that summarizes the ranges and the number of bytes commonly used in the representation of integers:

Signed / UnsignedNº 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 usually has a more negative value than positive for a reason, that positives include zero.

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

For example, in a 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.