How integers are stored in memory using two’s complement
Juan Sebastian Gonzalez
Software Developer | Full-Stack Developer | Javascript | React | Nextjs | Redux | Node.js | Python | Django | DRF | Nestjs | Fast Api | Docker | MYSQL | MONGO | ARANGODB
A computer is an amalgamation of hardware and software, where digital memories store information in bits (short for binary digits), the smallest piece of information used to quantify computer data. Bits can take on only two values: 1, which represents true (on), and 0, which represents false (off).
The computer processes data in the binary numeral system, but it can also interpret it in different number systems like decimal (base 10), hexadecimal (base 16), octal (base 8), and more (with the help of the right algorithm). Bits are used in different combinations, usually grouped in sets of 8 bits called bytes, to represent these numbers.
Programming languages like C allow developers to declare variables to store numeric data in the computer's memory. The number of bits required to represent integers (int) data types depends on the computer architecture and the compiler used. For instance, in a 16-bit computer architecture, the size of an integer number is 2 bytes, while it's 4 bytes in a 32-bit or 64-bit architecture. Please refer to the table below for more details.
In this article, we will discuss how positive and negative numbers are represented in a 32-bit or 64-bit architecture.
In a compiler, positive and negative numbers are differentiated by their sign bit, which is the most significant bit (MSB). When the MSB is zero, it indicates that the number is positive, and when it is one, it indicates that the number is negative. In a 32-bit system, the maximum value that can be stored is (231)-1, which takes up 31 bits in its representation. Therefore, the MSB is always zero for positive numbers. If all the bits are 1s, the number is -1.
Let's now discuss how positive integers are stored in memory. A positive integer is represented in binary notation, and each bit is stored in memory. For example, the decimal number 10 is represented as 1010 in binary notation. This number is stored at the end of the variable int and filled with zeros on the left side until it reaches all 32 bit positions. At the start of the variable, we have the MSB, which is 0 for positive numbers.
In summary, the MSB is a crucial indicator of whether a number is positive or negative, and positive integers are stored in memory bit by bit using binary notation.
An efficient method for converting binary numbers to base 10 involves performing a base 2 multiplication on each bit, starting from the rightmost bit and moving towards the left. By following this approach, you can obtain accurate and reliable results with ease.
How negative integers are stored in memory?
In computer programming, storing negative numbers requires a special representation using both ones (1) and zeros (0). The negative representation is achieved through a two-step process.
Step 1 - Ones Complement:
To obtain the ones complement of a number, we simply invert all its binary bits. This process results in a binary number with its 1s and 0s reversed.
Step 2 - Two's Complement:
Once we have the ones complement, we can then proceed to obtain the two's complement. This is done by adding 1 to the ones complement of the number.
By following these steps, we can accurately represent negative numbers in binary format and perform arithmetic operations on them with ease.
Step 2 - 2’s complement.
To get 2’s complement of a number, just add 1 to 1’s complement of an actual number.
At the start of this variable, we got the MSB (most significant bit), which in this case is 1. Representing negative.
The algorithm of the above image was used in a 64-b architecture, so we got at the end 64 bits to work with.
领英推荐
Numbers signed vs Numbers unsigned
As a developer, you may encounter the terms "signed" and "unsigned" when working with numbers. It's important to understand that these terms don't refer to any inherent property of the numbers themselves, but rather to how they are interpreted by the computer's central processing unit (CPU).
When the CPU moves a number from one location to another, it doesn't know whether that number is intended to be treated as signed or unsigned. Instead, it's up to the developer to provide the necessary context and distinguish between the two.
Fortunately, the CPU provides tools that make this distinction possible. By using these tools, you can ensure that your program handles signed and unsigned numbers correctly and avoids common errors that can arise from misinterpretation.
Arithmetic operations
Binary numbers support the same arithmetic operations as other numerical systems. However, unlike decimal or other base systems, binary numbers experience overflow when the value exceeds 1. In this scenario, the bit becomes 0 and is carried over to the next leftmost bit during addition.
Addition
Subtraction
In order to subtract negative numbers in a computer program, it is necessary to first convert the negative number to a positive interpretation using the 2's complement operation, which was explained earlier in this article. Once the subtraction is complete, the 1'st and 2'st complement operations are used again to ensure the correct interpretation of the result.
Other operations, such as multiplication and division, follow a similar workflow to that of the decimal system. However, when dealing with negative numbers in these operations, it is also necessary to first apply the 2's complement operation before obtaining the final result.
Overflow
When performing arithmetic operations, it's important to understand the differences between unsigned and signed arithmetic. In unsigned arithmetic, a carry of the most significant digit indicates an overflow. However, in signed arithmetic, detecting an overflow is more complex as it requires checking the consistency of the signs of the operands and the final answer.
It's essential to approach overflow detection with care in signed arithmetic to ensure the accuracy and reliability of the results. By understanding these differences, developers can write more efficient and effective code that handles arithmetic operations appropriately, regardless of whether they are working with signed or unsigned values.
Like was explained before the representation of the numbers depends on the architect of the computer, for example, if we got a number in the positive limit and addition is executed, appear an overflow and the system interpret it as a negative number.
This case is the same that in the previous example, but in this is passed the negative limit resulting in a positive number interpretation.
As we conclude this article, we have provided you with a fundamental understanding of how integers are stored based on computer architecture and how to work with them in binary notation.
We trust that this information has been insightful and valuable to our readers in enhancing their knowledge on this topic. Thank you for reading, and we hope you found this article useful.