Signed integer representation in binary

Pawan Acharya
codingmountain
Published in
4 min readMar 29, 2024

How does a computer understand the minus(-) sign?

binary calculation

Binary

I don’t have to stress out the fact that the computer understands and works on nothing but binary. Ultimately all the operations need addition, subtraction, multiplication, and division.

2 = 10
10 = 1010

2 + 10 = 10 + 1010 →1100 (Binary addition rules)
and 1100 = 12 in decimal representation

But numbers can both be positive and negative, today let’s dive into how the computer understands positive and negative numbers.

Number Line

number system in binary

The number line can be divided into 2 parts, left from 0 are negative numbers and right are positive numbers including 0.

The positive numbers are easy to represent for eg:

4 = 100
7 = 111

But how to represent -4,
is -4 = -100
No, the computer system has no minus(-) sign representation at the hardware level.

Why don’t we have a minus sign?

Let's recall this article

Here we discussed the physical layer of digital storage like flip-flops, registers, transistors, etc, which are based on voltage level or digital logic and can store and represent either 1 or 0. So there is no way they can store and represent the minus sign.

It’s not like we cannot do negative operations in binary, but they are all abstracted from the physical layer.

Solution

There are smart ways of representing negative numbers, allowing us to utilize existing hardware solutions to represent bits. We will discuss two of them.

Sign Magnitude

From the name, it says that any binary number will be represented by a combination of sign + magnitude(value).

The sign bit is the first bit or MSB (Most Significant Bit) of the number and the remaining will represent magnitude.

Where +ve is represented by → 0, i.e starts with 0
and -ve is represented by → 1, i.e starts with 1

Let's take an example

010
Here, the first bit is 0 which means the number is +ve and the remaining is 10, and in decimal 10 is 2
So 010 represents +2

110
Here, the first bit is 1 which means the number is -ve and the remaining bit is 10 in decimal 10 is 2
So 110 represents -2

Simple right?
So when we write code with negative number, the compiler will convert to this reperesentation and in Assembly level all the arithmetic operation algorithms respect this convention in a way that they seamlessly handle both positive and negative integers.

Another example
100
Here, the first bit is 1 which means the number should be -ve and the remaining 00 in decimal is 0
So 100 represent -0 but there is nothing such as -0

So this method fails and also wastes one bit to represent signs.

2’s Complement

This is the standard way of representing negative numbers, the concept is the same, we look for the first bit, if it is 1 we will consider the number negative and to find the actual value we will find 2's complement.

If the first bit is 0, then the number is positive and we will use that value directly.

2's complement calculation

Step 1. Complement all the bits i.e 0 -> 1 and 1 -> 0
Step 2. Add 1 to the result of step 1

Shortcut: Moving from right(LSB) to left(MSB) copy all bits until
you encounter first 1, then after that 1 complement all the bits.

Examples:

010
Here, the first bit is 0 means +ve so we do nothing and consider the number itself i.e 010 in binary means +2 in decimal.

110
Here, the first bit is 1, which means the number is -ve so we need to find 2’s complement of the number which will be 010.
010 = 2 in decimal and we can say 110 in binary represent -2 in decimal.

Let's take the case that failed in the Sign Magnitude representation

100
Here, the first bit is 1 which means the number is -ve so we need to find 2’s complement which will be 100.

100 = 4 in decimal so, 100 in binary represents -4 in decimal.

3 bit binary negative number representation with 2’s complement method.

So, with 3-bit signed int, we get range -4, -3, -2, -1, 0, 1, 2, 3

Unsigned int

If we consider unsigned int we get more range of the numbers i.e with 3-bit we can get 0, 1, 2, 3, 4, 5, 6, 7

So yes, this is how assembly-level programs understand and agree on the positive and negative numbers.

--

--