HOW NEGATIVE NUMBERS ARE STORED IN MEMORY, C++?
In this article we will learn How negative numbers are stored in memory.
Numbers form an integral part of all calculations and
operations. In any programming language numbers play the most important job of
being the operands on which the operators can work. Without numbers, the
program will be like a complete machinery without the raw material to process.
As numbers are such an important part of a programming language it becomes
necessary to have a clear understanding of how these numbers interact with the
compiler.
We all know that numbers from any number system are stored in computer in their equivalent binary form.
HOW NEGATIVE NUMBERS ARE STORED IN MEMORY?
It is easy to understand conversion from a positive decimal
(base 10) number to binary. A positive number is converted to binary, and the
binary form is stored in the memory. The number of bits available for storage
depends on the datatype of the variable to which the number is assigned.
But, if we enter a negative integer, what does the compiler
do?
Well there are 3 ways to store negative numbers (note: we will be using 8 bit or 1 byte capacity in all our examples because it’s easier to understand) :
SIGNED MAGNITUDE
In this form of storing negative numbers, the
Most Significant Bit (i.e. the leftmost digit in the binary code) represents
the sign of the number. If MSB (Most Significant Bit) is ‘0’ it denotes a
positive number and if MSB is ‘1’ it denotes a negative number.
Let us see how 23 and -23 are stored:
Step 1: The absolute or the mod value of the
number is converted to binary
23=(10111)2
Step 2: The MSB is put ‘0’ for positive &
‘1’ for negative (remember we are using 8 bit space)
When the compiler has to convert the binary code to decimal form, it reads the value at MSB. If it is found 0 then it puts a ‘+’(plus) sign and then converts the remaining 7 bits into decimal number. If the value at MSB is 1, the compiler assigns ‘-‘ (minus) sign and then converts the remaining 7 bits into the decimal form.
Range of numbers in 8 bit form:
As the MSB is insignificant to the magnitude
of the number, the maximum number that can be stored in this form is 27-1
or 127. These numbers actually range from 00000001 to 01111111 (note : the ‘0’
at MSB only represents positive number).
The minimum number that can be stored as
signed magnitude (8 bit) is –(27-1)
or -127. These numbers actually range from 10000001 to 11111111 (note : the ‘1’
at MSB only represents positive number and has no role in the magnitude of the
number)
Zero in this form can be represented in two
ways viz. 10000000 & 00000000.
Thus, the range of 8 bit signed magnitude is
-127 to +127.
1’s COMPLIMENT FORM
In this form, a positive number is stored in its normal binary form but a negative number is stored as the compliment of the binary form of its absolute value.
Suppose we have to store -23 in 1’s compliment form. The steps are:
Step 1: Find the binary equivalent of 23
(i.e. the absolute value of the number):
23=(00010111)2
Step 2: Find 1’s compliment of the binary
equivalent :
~(00010111)2 =
(11101000)2
Therefore, -23 is stored as 11101000.
When the compiler needs to retrieve the
number, it looks at MSB. If the digit at MSB is 0 then it indicates positive
number and the compiler simply converts it into the decimal form.
But, if the digit at MSB is 1, the compiler
performs compliment operation and then converts the resulting binary into
decimal number. It also puts a – (minus) sign in front.
Range of numbers in 8 bit form:
The maximum positive number that can be
stored in 1’s compliment form is (01111111)2 which is equivalent to
127 in decimal number system. Making the MSB ‘1’ will reverse the sign of the
number as the compiler will take it as a negative number.
The smallest number in 1’s complement form can
be (10000000)2 which is equivalent to -127. ‘1’ at MSB represents
that the number must be negative. Any modifications in the remaining bits will
result in a number greater than -127.
Zero in 1’s compliment form is represented as
(00000000)2
Thus, the range of numbers is from -127 to 127.
2’s COMPLIMENT FORM
In this form, positive number is stored as the simple binary equivalent of the number. But a negative number is stored as the 2’s compliment of the binary equivalent of absolute value of that number.
Suppose we have to store -23 in 2’s
compliment form. The steps are:
Step 1: Find the binary equivalent of 23
(i.e. the absolute value of the number):
23=(00010111)2
Step 2: Find 2’s compliment of the binary
equivalent:
~(00010111)2 + 1
= (11101001)2
Therefore, -23 is stored as (11101001)2
When the compiler needs to retrieve the
number back, it reads the MSB. If it is ‘0’ , the compiler converts the binary
directly to its decimal equivalent.
But, if the digit in MSB is ‘1’ , the
compiler takes 2’s compliment of the stored binary and then converts it into
decimal equivalent, putting a – (minus) sign.
Range of numbers in 8 bit form:
The maximum positive number that can be
stored in 2’s compliment form is (01111111)2 which is equivalent to
127 in decimal. Making the MSB ‘1’ will reverse the sign and compiler will take
it as a negative number.
The minimum number that can be stored in 2’s compliment
form is (10000000)2 which the compiler will read as -128. Any
modifications will result is a number greater than -128.
Zero in 2’s compliment is represented as (00000000)2
Thus, the range of 8 bit numbers is from -128
to 127.
All the modern compilers use 2’s compliment form to store the negative numbers as it gives a larger range and is comparatively easy for the compiler to process.
If you find something wrong or if you
wish to add some information in this article, feel free to comment on this post
or contact us at coding.nkcoder@gmail.com.
Which topics you want an article on? Comment down on this
post.
Do not stop here. Read all articles related to C++ here.
If you want to contribute any article for the website, feel
free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Related posts:
What happens when a number beyond the range is stored in a variable?
Post a Comment