In this post, I will try to explain different representations of numbers in both binary and decimal system.
Decimal System
- Any number can be represented in the sum of multiples of powers of 10
- Base 10 system, needs numerals (0-9)
- Standard system for denoting numbers
Binary System
- Any number can be represented in the sum of multiples of powers of 2
- Base 2 system, needs numerals (0,1)
- Machine level
Integers
An integer is a number that is written without any fractional part, it can be positive, negative or zero.
Let us represent an integer 261 in both decimal and binary system
261 = 2*(10^2) + 6*(10^1) + 1*(10^0)
261 = 1*(2^8) + 0*(2^7) + 0*(2^6) + 0*(2^5) + 0*(2^4) + 0*(2^3) + 1*(2^2 ) + 0*(2^1) + 1*(2^0)
100000101
MSB = Most Significant Bit (Red)
LSB = Least Significant Bit (Blue)
What about Negative Integers?
In the decimal system, we use the "-" symbol to distinguish between positive and negative numbers, whereas In the binary system we use an extra bit at the start (i.e MSB) as a sign bit. if MSB is "1" it is negative and if MSB is "0" it is positive this is also called as Signed representation.
But as we can see from the above figure, using this method is not efficient as there is data wastage. So to avoid such a problem we use two's complement.
One's Complement:
Inverting all bits (swapping 1's and 0's)
Example: 1010's one's complement is 0101
Two's Complement
One's complement of a number is added with 1
Example: 1010's one's complement is 0110
Comments
Post a Comment