Storing of Integer Numbers
Integer: These numbers are further divided into two types
- Signed Integers
- Unsigned Integers.
To store unsigned integers simple binary format is used.
let a = 4
let b = 78
a will be stored in the memory with the value of:- 100
b will be stored in the memory with the value of:- 1001110
Note: This method fails when working on unsigned integers as extra data is required to store the sign of numbers.
There is a convention to store the leftmost bit as a sign bit and use 0 for positive and 1 if negative number.
This method is known as Signed Magnitude
The below example illustrates how we will store the number in Signed Magnitude format.
let a = 6
let b = -6
a will be stored in memory with the value of :- 000110
b will be stored in memory with the value of :- 100110
A problem arises when we try to perform addition on these two values. the addition performed on the binary numbers returns
100110 + 000110 = 101100 // 44
We should get 0 when we add the two numbers but instead, we get 44
A further improvement of this method was implemented and one’s complement representation was used to store the number
One’s complement of a number is toggling all the 0’s into 1 and 1’s to 0. Suppose there is a binary number 11001001, then its one’s complement will be 00110110.
Now let us understand how this method is used to store numbers in the memory.
Let us suppose the number in binary format is 100111, which will be treated as a negative number since 1 is the leftmost bit. Now its one’s complement is 011000 which is 24 in the decimal format so we will treat 100111 as -24.
In this method, the problem which arose in the previous method is partially solved as addition now starts giving correct results but there is still some exceptions such as when we add 3 and -2 using this representation we get wrong output
3 -> 000011
After addition we get -> 000000 which is +0
Output was expected to be 1.
To further enhance this approach the numbers two complement format was introduced
Two’s complement of a number is similar to One’s complement with one extra step that after finding the one’s complement of the number 1 is added again to the result.
The two complements in the previous of 24(011000) will be 101000. This number when added with the original number 24 we get 000000 as output where the carry bit is ignored and the arithmetic operation passes.
24 -> 011000
two's complement of 24 -> 101000
Addition gives -> 1000000
// One is ignored as digits upto 6 bits are counted
So -24 will be stored as 101000 in the memory
When storing Integers in unsigned bit simple binary format is followed
In order to store signed bit integers, the numbers internally will be stored as two’s complement so as to increase the storage capacity and improve arithmetic calculation.
Storing of Floating point numbers.
To store a float number we divide it into three parts sign bit, exponent, and mantissa.
- sign bit: It is used to indicate a sign of a number with convention as 0 for positive and 1 for negative.
- exponent: It is the difference between a real binary number and its normalized form.
- mantissa: It is used to store the fraction part of the floating point number’s normalized form.
Floating point numbers represented using 32-bit format are called single precision whereas the numbers represented using 64-bit format are called double precision
Storage Space required for storing number in different formats:
The concept of bias is used in the floating point representation to represent negative numbers too instead of one’s and two’s complement. Bias is used because comparison becomes difficult in the case of floating point numbers with two’s complement representations.
For eg., a normal 6-bit representation will follow numbers from 000000 to 111111 but IEEE has given a formula that will store the negative numbers too. If n is the number of bits the formula will be.
bias = 2(n-1) - 1
// Here n = 6
therefore bias = 31
This means in 6-bit representation we can store numbers from -31 to 32
Let us see how the binary number 101.101 will be represented in scientific notation.
we will get 1.01101 * 2^2
- Here, the sign bit is 0 and will be the leftmost bit
- The exponent is 2. It will indicate the distance between the original binary number and the normalized form.
- The mantissa is the fraction part which will be 01101
Storing of Irrational Numbers.
Computers are still incapable to store and manipulate irrational numbers so they cannot be stored.
- The floating point numbers are not always accurate and give approximate results.
Example: In this example, we will try to understand the above problem.
let x = 0.1;
let y = 0.2;
Output: Here, instead of real calculation an approximate result is given for fast performance
- Floating numbers give different output depending on their associativity
Example: We will understand the associativity problem with the example given below.
let x = 0.1;
let y = 0.2;
let z = 0.3
Output: Since floating point calculations are based on rounding, therefore, the result is not always the same even though it should be the same in real-world applications.
Share your thoughts in the comments
Please Login to comment...