السَّلاَمُ
عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
NUMBER SYSTEM
CONVERSION
hello,today i gonna teach you how to convert :
- decimal - binary
- binary - decimal
- decimal - hexadecimal
- hexadecimal - decimal
1 . How to
convert decimal to binary
In order to convert decimal number x to binary number:
For decimal number x:
- Get the highest power of 2 that is less than the decimal number x:2n < x, (n=1,2,3,...)
- The high binary digit is equal 1:dn = 1
- Calculate the difference Δ of the number x and he power of 2, 2n:Δ = x - 2n
- Repeat step #1 with the difference until the result is 0.
for the example :
Convert x=13 to binary.
Solution :
n=3, 23=8 < 13
n=4, 24=16 > 13
So
n = 3
d3 = 1
Δ = 13 - 23 = 5
n = 2, x = Δ = 5
d2 = 1
Δ = 5 - 22 = 1
n = 1, x = Δ = 1
d2 = 0
n = 0, x = Δ = 1
d0 = 1
Δ = 1 - 1 = 0
(d3d2d1d0) = 1101
So 13 in decimal is equal to 1101 in binary:
x = 1310 = 11012
this video shown to convert decimal to binary in easier way.
2 . How to convert binary to decimal
The decimal number is equal to the sum of powers of 2 of the binary number's '1' digits place:
binary number: | 1 | 1 | 1 | 0 | 0 | 1 |
---|---|---|---|---|---|---|
power of 2: | 25 | 24 | 23 | 22 | 21 | 20 |
1110012 = 1·25+1·24+1·23+0·22+0·21+1·20 = 57
this video is show how to convert binary to decimal
3 . How to convert from
decimal to hexadecimal
For decimal number x:
- Get the highest power of 16 that is less than the decimal number x:16n < x, (n=1,2,3,...)
- The high hex digit is equal to the integer if the decimal number x divided by the highest power of 16 that is smaller than x:dn = int(x / 16n)
- Calculate the difference Δ of the number x and the hex digit dn times the power of 16, 16n:Δ = x - dn × 16n
- Repeat step #1 with the difference result until the result is 0.
for example :
Convert x=603 to hex:
n=2, 162=256 < 603
n=3, 163=4096 > 603
So
n = 2
d2 = int(603 / 162) = 2
Δ = 603 - 2×162 = 91
n = 1, x = Δ = 91
d1 = int(91 / 161) = 5
Δ = 91 - 5×161 = 11
n = 0, x = Δ = 11
d0 = int(11 / 160) = 1110 = B16
Δ = 11 - 11×160 = 0
(d2d1d0) = 25B
So
x = 60310 = 25B16
this video shown how to convert decimal to hexadecimal
4 . How to convert from
hexadecimal to decimal
A regular decimal number is the sum of the digits multiplied with its power of 10.
Example #1 :
137 in base 10 is equal to each digit multiplied with its corresponding power of 10:
13710 = 1×102+3×101+7×100 = 100+30+7
Hex numbers are read the same way, but each digit counts the power of 16 instead of power of 10.
Multiply each digit of the hex number with its corresponding power of 16.
Example #2 :
3B in base 16 is equal to each digit multiplied with its corresponding power of 16:
3B16 = 3×161+11×160 = 48+11 = 59
Example #3 :
E7A9 in base 16 is equal to each digit multiplied with its corresponding power of 16:
E7A916 = 14×163+7×162+10×161+9×160 = 57344+1792+160+9 =59305
this video shown how to convert hexadecimal to decimal
2's complement number
- Positive Numbers
- Negative Numbers
Integer | 2's Complement | |
---|---|---|
Signed | Unsigned | |
5 | 5 | 0000 0101 |
4 | 4 | 0000 0100 |
3 | 3 | 0000 0011 |
2 | 2 | 0000 0010 |
1 | 1 | 0000 0001 |
0 | 0 | 0000 0000 |
-1 | 255 | 1111 1111 |
-2 | 254 | 1111 1110 |
-3 | 253 | 1111 1101 |
-4 | 252 | 1111 1100 |
-5 | 251 | 1111 1011 |
- Note: The most significant (leftmost) bit indicates the sign of the integer; therefore it is sometimes called the sign bit.
-
- If the sign bit is zero,
- If the sign bit is one,
- then the number is less than zero, or negative.
then the number is greater than or equal to zero, or positive.
Calculation of 2's Complement
To calculate the 2's complement of an integer, invert the binary equivalent of the number by changing all of the ones to zeroes and all of the zeroes to ones (also called 1's complement), and then add one.
For example,
0001 0001(binary 17) 1110 1111(two's complement -17) | ||
NOT(0001 0001) | = | 1110 1110 (Invert bits) |
1110 1110 + 0000 0001 | = | 1110 1111 (Add 1) |
2's Complement Addition
Two's complement addition follows the same rules as binary addition.
For example,
5 + (-3) = 2 | 0000 0101 | = | +5 | |
+ 1111 1101 | = | -3 | ||
0000 0010 | = | +2 |
2's Complement Subtraction
Two's complement subtraction is the binary addition of the minuend to the 2's complement of the subtrahend (adding a negative number is the same as subtracting a positive one).
For example,
7 - 12 = (-5) | 0000 0111 | = | +7 | |
+ 1111 0100 | = | -12 | ||
1111 1011 | = | -5 |
2's Complement Multiplication
Two's complement multiplication follows the same rules as binary multiplication.
For example,
(-4) × 4 = (-16) | 1111 1100 | = | -4 | |
× 0000 0100 | = | +4 | ||
1111 0000 | = | -16 |
2's Complement Division
Two's complement division is repeated 2's complement subtraction. The 2's complement of the divisor is calculated, then added to the dividend. For the next subtraction cycle, the quotient replaces the dividend. This repeats until the quotient is too small for subtraction or is zero, then it becomes the remainder. The final answer is the total of subtraction cycles plus the remainder.
For example,
7 ÷ 3 = 2 remainder 1 | 0000 0111 | = | +7 | 0000 0100 | = | +4 | ||
+ 1111 1101 | = | -3 | + 1111 1101 | = | -3 | |||
0000 0100 | = | +4 | 0000 0001 | = | +1 (remainder) |
Sign Extension
To extend a signed integer from 8 bits to 16 bits or from 16 bits to 32 bits, append additional bits on the left side of the number. Fill each extra bit with the value of the smaller number's most significant bit (the sign bit).
For example,
Signed Integer | 8-bit Representation | 16-bit Representation |
---|---|---|
-1 | 1111 1111 | 1111 1111 1111 1111 |
+1 | 0000 0001 | 0000 0000 0000 0001 |
posted by < MOHD SYUKRI BIN MAT LAZIM >
< B031310013 >
No comments:
Post a Comment