How Two Years of Podcasting Changed My Life

My Food Job Rocks! now has 120+ episodes, 500+ facebook likes, 45,000+ downloads, and some really awesome connections, this was one of the best decisions that have drastically changed my life. Going…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Integer numbers storage in computer memory

Fig 1. The four-bit two’s complement representation circle. Source: [1]

Computer machines are composed by digital memories that store information in bits (binary digit). It means that a bit is an unit — the smallest one — used to quantify computer data [2]. The bit represents a logical value, which corresponds to a two-state device. These states are often represented as 0 or 1, and could be interpreted as “on/off”, “true/false”, etc.

On the other hand, a bit is a digit of the binary numeral system. Through this numeral system, and the combination of bits, any integer belonging to the decimal numeral system can be represented. As digital information are stored in bits, computers use binary numeral system to represent all numbers — integers, octals, hexadecimals. A byte is commonly known as a group of 8 bits.

In programming languages, like C language, it is possible to declare variables in order to store numeric data in the computer memory. The number of bits that represent integers (int) data type can change according to the computer architecture, or processing, and the compiler. In a 16-bit machine, the size of an integer is of 2 bytes, but in a 32-bit or 64-bit machine, the size is of 4 bytes. The values range of integers are shown in the following table [3]:

There are three data types that allow to store integers values: int, short and unsigned. According to the storage size of each data type, a short integer is represented by 16 bits, while an unsigned integer is represented by 32 bits. With the purpose of using enough computer memory, each data type is used according to the value range of the stored numbers.

Supposing there is a C program — compiled in 64-bits — where will be declared an integer variable with a value of 25 as shown below:

When this code is executed, where a.out is the file name, the output will be the printed value of the variable:

But, as it was explained above, this integer is stored as a binary number by the computer memory:

Fig 2. Illustration of the storing of an integer, with a size of 4 bytes, in the computer memory (RAM).

This way it works with an unsigned and a short integer, although the last mentioned uses 16 bits. However, there are some differences when a negative value is assigned to an integer variables.

A binary with a static size represents an integer within a value range determined. For unsigned binary numbers, this value range goes from 0 to the value obtained when all digits are equally to “1”. With 2 and 4 bytes, the maximum integer value obtained by a binary of 2 and 4 bytes are shown below:

Fig 3. Maximum integer values obtained from 2 bytes (16 bits) and 4 bytes (32 bits).

It means that their value range are determined by the following mathematical notation:

Fig 4. Mathematical notation that represents the value range of an unsigned integer obtained from a binary.

This is the way computer memory interprets unsigned integers. But for signed integers, in which a dash (“-”) is written before number when it is negative, it changes. There are some methods to represent negative numbers from a binary.

An integer number is identified as positive or negative if it has or does not have a dash before. In other words, this number only have of two-states. Each state can be represented through a bit of the binary number. This is called as Most Significant Bit (MSB): if the number is positive, this has a value of 0, but if the number is negative, the bit is 1. The entire number is called a signed binary number.

It has some disadvantages:

Fig 6. Mathematical notation that represents the value range of integers obtained from a signed binary number.
Fig 7. Positive and negative zero obtained from a signed binary number.

It is a method that represents a negative number through of inverting the values of bits of the number magnitude. Since the value first bit (also called sign bit) of the positive number is 0, when the inversion is done, this first bit is equal to 1 to indicate a negative number. The sign binary number obtained from the inversion is called complement.

The following image is an example of the process of inversion with one’s complement:

The value range of integers obtained from one’s complement is the following:

Fig 9. Mathematical notation that represents the value range of integers obtained from one’s complement method.

In comparison with the value range obtained with a signed binary number and its MSB, this method allows one more possible value. However, through this method a negative zero is still obtained:

Fig 10. Positive zero, lower value and negative zero in signed binary number obtained from one’s complement, being n the number of bits (32).

It is a method to represent negative integers, similar to one’s complement. It also inverts the magnitude of the negative number but, then, it is added one (+ 1) to the complement number [4]. It has a sign bit that allows to indicate if a number is positive or negative.

Fig 11. Signed binary number (-25) obtained from two’s complement method.

This method allows addition and subtraction with positive and negative numbers. Compared with one’s complement, the subtraction with two’s complement of two numbers , which has same magnitude value, is equal to zero.

The value range of integers obtained from two’s complement is the same as the obtained from one’s complement, shown in the Fig 9. With this method, it is not obtained a negative zero (-0), since it is added one to the zero-inverted number:

Fig 12. Signed binary number of zero (0) obtained from two’s complement method.

For the reasons explained above, this method is used by computer memory to store signed integers.

Supposing there is a C program that tries to print negative numbers with an integer variable (called inum) and an unsinged integer variable (called unum), like the following:

The output of this program is:

It is known that unsigned integer variables only store positive numbers, but when a negative number is casted to this data type, the result is a positive number, even higher than its magnitude value — in this case, 25.

Fig 13. Binary numbers of the integers printed in the example.

In this case, the process made by the computer consisted in determining the signed binary number of the negative integer through two’s complement method. Then, when the number was casted to an unsigned integer, the binary number was assumed as an unsigned binary number. As shown in Fig 13, the signed number binary of -25 is the same than the unsigned binary number of 4294967271.

Add a comment

Related posts:

15 Secretly Funny People Working in Train Sim World 2020 gratuit

In some cases operate could be the worst point in everyday life, it is usually seen as practically nothing in excess of a means to an end. Usually the only thing that you actually think of is, what…

Talk to your students about academic integrity.

With the increased response to COVID-19 there has been a concurrent escalation in predatory and unethical practices such as price gouging on hand sanitizer and cleaning products on Amazon and online…

Finding and applying for my short work placement

Originally the thought of finding and applying for a work placement filled me with dread, as applications and interviews are something I avoid at all costs. My career self-efficacy score had…