Representation of numbers

So far we have only dealt with logical variables and performed logical operations. We will see in this section how to represent and manipulate numbers.

Natural numbers

General Representation

A natural number can be represented in a base by a -tuple such that 1:

In this representation:

  • is a digit of rank and belongs to a set of symbols ( to )

  • is called the most significant digit

  • is called the least significant digit

The most commonly used bases2 are:

  • : decimal representation,

  • : hexadecimal representation,

  • : octal representation,

  • : binary representation, . A binary digit is also called bit (short for binary digit)

Unsigned binary representation

We will focus in the following on the representation of numbers in binary (base 2). In this base, an unsigned integer can be represented by the -tuple such that:

Where:

  • is a bit (it belongs to a set of symbols: or )

  • is called the most significant bit (MSB)

  • is called the least significant bit (LSB)

Conversion between hexadecimal and binary

It is very simple to go from a number represented in hexadecimal to a number represented as an unsigned binary. Just concatenate the 4-bit binary representation of each of the hexadecimal digits.

Example:

We have:

Similarly, to go from an unsigned binary representation to a hexadecimal representation, it suffices to group the bits 4 by 4 (starting from the least significant bits), thereby transforming each of the groups into a hexadecimal digit.

Example:

Modulo binary representation

In practice, in hardware, numbers are represented and manipulated over a finite fixed number of bits. So a number will be represented, modulo , on bits. Using bits, it is therefore possible to represent the natural numbers in the range ].

Signed integers

There are several methods to represent signed integers. The most commonly used is the representation in two’s complement (CA2).

As seen previously, using bits, numbers are represented modulo , i.e. has the same representation as , has the same representation as , etc.

If we extend this principle to negative numbers, we would require to have the same representation as (i.e. ), that has the same representation as (i.e. ), etc.

By convention, in the 2’s complement representation, the numbers whose most significant bit is 1 will be considered as negative numbers, and the numbers whose most significant bit is 0 will be considered as positive numbers3.

In two’s complement, we can therefore represent, using bits, the signed numbers in the range ].

Example: 4-bit representation ():

CA2 BinaryDecimalUnsigned BinaryDecimal

From a mathematical point of view, a number represented as two’s complement with bits by is given as:

Sign extension

Extension rules allow us to go from a number represented with bits to the same number represented with bits.

Unsigned integers

In the unsigned case (natural numbers), the extension consists of adding bits valued on the left (most significant bits).

Example: represented as an unsigned integer using bits gives us . Using bits, it is represented as .

Two’s complement

In the case of a signed number represented in two’s complement, the extension is a little more complex because we must not forget that the most significant bit carries the information of the sign. For a two’s complement number, the extension is done by duplicating the most significant bit.

Examples: is represented with bits in two’s complement in the form . On bits, it is represented as . is represented with bits in two’s complement as . On bits, it turns out as .

Proof: Let be a signed integer represented in two’s complement using bits.

Hence is its representation with bits.

Summary

In conclusion, when we represent a number in binary, it is very important to specify the convention chosen (unsigned, two’s complement, fixed point…) as well as the number of bits.

1

Note that the operators and take on their classical mathematical meaning here, i.e. addition and multiplication, unlike in the previous chapter where they corresponded to the logical operators OR and AND.

2

In the following, in case of risk of confusion, the base in which a number is represented will be indicated as a subscript, example: , ,

3

This keeps compatibility with the unsigned representation

4

Fixed because once the respective number of bits of the integer part and the fractional part are fixed, they do not change. There are other methods, including the floating-point representation — which you’ve probably heard of — which is significantly more complex to implement at the operational level and which we will therefore not discuss in this course.