I'm trying to convert decimal numbers with commas into binary. I've seen that if you have for example the number 5.2 in decimal you convert it to 101.10, since 5 is 101 and 2 is 10 but I've seen a method that I don't quite understand.
For example, the number 93.3125 is 0000 0101 1101 0101. Why ?
Are there other methods that help you transform decimals with comma into binary ?
You need to handle each part (before and after decimal point) separately. The integer part is converted by consequent division by base and the decimal part by consequent multiplying by base. In your case base = 2:
5.2 dec -> 5 + 0.2 dec
Integer part
5 % 2 = 1 // binary digit
5 / 2 = 2 // leftover for next iteration
2 % 2 = 0 // binary digit
2 / 2 = 1 // leftover for next iteration
1 % 2 = 1 // binary digit
1 / 2 = 0 // leftover for next iteration
Do not forget that the digits has to be used in reverse order (your 5 is the same however):
5 dec = 1 0 1 bin
Decimal part
0.2 * 2 = 0.4
0.4 * 2 = 0.8
0.8 * 2 = 1.6
0.6 * 2 = 1.2
0.2 * 2 = 0.4
...
All is in decadic. The integer part of result is your digit (in order) and the decimal part is leftover for next iteration. So the result is:
0.2 dec = 0 . 0 0 1 1 0 ... bin
Putting together:
5.2 dec = 101.00110... bin
There are a multitude of ways to convert a decimal number into a string of 1s and 0s. You have just demonstrated two of them.
If you convert the number (represented in base-10 place notation) 93.3125 into "binary" (represented in base-2 place notation) you get 1011101.0101
If you convert it into a a binary floating-point format used in a computer language, you get a string of 1s and 0s, perhaps with a leading bit indicating sign, and some other bits for exponent -- you'll need a format description document to decode it.
Translating "5.2" into "101.10" (translating the parts before and after the decimal point separately) is a new one on me, and it is not a mathematically correct translation of place notation. "5"=>"101" is correct, but ".2"=>".10" is not; ".2" in decimal is two tenths, a.k.a. one fifth, which in base two is ".001100110011..." (one fifth is a repeating decimal in base two, just as one third is a repeating decimal in base ten.) And how would you translate "1.02"?
So yes, there are plenty of ways to encode a number in binary. Different codes are useful in different ways.
Related
I'm a student in programmation and I have a course called "Informatic Mathematics". In the exercices it's asked to convert floating numbers from decimal, octal, hexadecimal or binary to another base (not necesserly to base 10) and keep 12 digits after the comma(or the dot) if it is possible. For exemple:
(135.263)b10 => b2
(100101001.11)b2 => b10
(1011110010.0101)b2 => b8
...
I know how to convert numbers. The way I convert the decimal part (after the dot) is to divide this part by the highest multiple of the target base until I get 0 or until I reach the 12th digits after the dot. The problem is that I don't know all the negate multiples of 2 so usually I write them on a separate sheet, but usually I don't have to keep 12 digits after the dot and writing these multiples on a seperate sheet takes time and during the exam, time is a precious thing and I can't waste it to write these multiples.
So I would like to know if there's a better way to do these conversions or if anyone has any tips.
Also, when I convert from non-decimal number to another non-decimal number (ex: b2 => b8) I usually convert the first number to base 10 and then convert the base 10 number to the target base. I would like to know if there's a way to convert the first number directly into the target base without having to convert it in base 10 first.
BTW: Sorry if my english is a bit weird. I'm a french canadian and I did my best, but please let me know if there is something you do not understand well.
I'll start with b2 > b8.
001 011 110 010.010 100
As you see, I've separated the number into 3 digit segments (2^3 = 8). You have to add extra 0 to the left and to the right to make it like that. Then you convert it digit by digit. In this case you'll receive 1352.24
b2 => b10
Some harder math here. Mark digits in your number this way:
1 0 0 1 0 1 0 0 1 . 1 1
8 7 6 5 4 3 2 1 0 -1 -2
Then calculate fractional and whole part
2^0 + 2^3 + 2^5 + 2^8 + 2^-1 + 2^-2
b10 => b2
Multiple the fraction by 2 till you get 1. From each multiplication you take the whole part. Example:
0.25 * 2 = 0.5; 0.5 * 2 = 1;
Thus, 0.25 is 0.01;
UPD For negative conversions check out first and second complement.
How to represent 500.2 in binary number system. I want to know the conversion method. I know how to convert numbers without points but if point comes in any number I don't know how to convert it.
Quoting the conversion description from Modern Digital Electronics 4E
Decimal to Binary conversion :
Any decimal number can be converted into its equivalent binary number.
For integers, the conversion is obtained by continuous division by 2
and keeping track of the remainders, while for fractional parts, the
conversion is affected by continuous multiplication by 2 and keeping
track of the integers generated.
The conversion process in your case is illustrated below :-
500/2 = 250 Remainder = 0
250/2 = 125 Reaminder = 0
125/2 = 62 Remainder = 1
62/2 = 31 Remainder = 0
31/2 = 15 Remainder = 1
15/2 = 7 Remainder = 1
7/2 = 3 Remainder = 1
3/2 = 1 Remainder = 1
1/2 = 0 Remainder = 1
So, the order of evaluation is that topmost remainder will go to LSB, the bottom-most remainder would go to MSB.
Therefore, (500)2 = 111110100.
Now, talking about the fractional part, we would go as follows :-
// separate the integer generated(0 or 1) on the left hand side of the fraction/dot,
// and ensure only fractional part between 0 and 1 are allowed in the next step
0.2 * 2 = 0.4 , so, keep 0 in the bag
0.4 * 2 = 0.8 , so, keep 0 in the bag
0.8 * 2 = 1.6 , so, keep 1 in the bag, and next put 0.6 to the next step
0.6 * 2 = 1.2, so, keep 1 in the bag, and next put 0.2 to the next step
0.2 * 2 = 0.4, so, keep 0 in the bag...
// and so on as we see that it would continue(repeating) the same pattern.
As we find that the series would go on infinitely, we can consider only the precision upto certain decimal places.
So, if I assume that the required precision is 4 digits after the dot, then the answer would be the sequence in which the digits are being placed in the bag, i.e.,
(0.2)2 = 0.00110011...
= 0.0011....
= 0.0011.
Now, combinedly, (500.2)2 = 111110100.0011 .
Here is a good webpage about it. I don't know if it answers your question :
http://www.h-schmidt.net/FloatConverter/IEEE754.html
Edit
from that link
Usage: You can either convert a number by choosing its binary representation in the button-bar, the other fields will be updated immediately. Or you can enter a binary number, a hexnumber or the decimal representation into the corresponding textfield and press return to update the other fields. To make it easier to spot eventual rounding errors, the selected float number is displayed after conversion to double precision.
Special Values: You can enter the words "Infinity", "-Infinity" or "NaN" to get the corresponding special values for IEEE-754. Please note there are two kinds of zero: +0 and -0.
Conversion: The value of a IEEE-754 number is computed as:
sign * 2exponent * mantissa
The sign is stored in bit 32. The exponent can be computed from bits 24-31 by subtracting 127. The mantissa (also known as significand or fraction) is stored in bits 1-23. An invisible leading bit (i.e. it is not actually stored) with value 1.0 is placed in front, then bit 23 has a value of 1/2, bit 22 has value 1/4 etc. As a result, the mantissa has a value between 1.0 and 2. If the exponent reaches -127 (binary 00000000), the leading 1 is no longer used to enable gradual underflow.
Underflow: If the exponent has minimum value (all zero), special rules for denormalized values are followed. The exponent value is set to 2-126 while the "invisible" leading bit for the mantissa is no longer used. The range of the mantissa is now [0:1).
Note: The converter used to show denormalized exponents as 2-127 and a denormalized mantissa range [0:2). This is effectively identical to the values above, with a factor of two shifted between exponent and mantissa. However this confused people and was therefore changed (2015-09-26).
Rounding errors: Not every decimal number can be expressed exactly as a floating point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit.
Other representations: The hex representation is just the integer value of the bitstring printed as hex. Don't confuse this with true hexadecimal floating point values in the style of 0xab.12ef.
I've got a question to which I need someone to help me verify to see if my answer is correct or not.
Showing all steps involved, convert the unsigned decimal number 28.2 to:
Hexadecimal, using 1 digit for the integer part and 2 for the fractional part.
I come up with the answer C1.33, but since I am only allowed 1 digit for the integer part I would assume I would remove the 1?
Hex, or hexadecimal, is a number system of base 16.hex digits are
0,1,2,3,4,5,6,7,8 and 9 A, B, C, D, E, F
decimal hex
C1 28
C 12
1 1
For 28 is C1 in hex, you cannot represent it with either c or 1
Please refer: link
Since I knew how to manually convert hexadecimal to decimal using this method.
Read from right to left, the last digit multiplied by the constant value 16 and plus the first digit.
For example:
12h = 2 + (1 * 16) = 18
99h = 9 + (9 * 16) = 153
How do I convert back into hex from decimal?
As you can see in the picture above. You need to draw a table in your brain
Lets take 456 as example.
If we divide 456 by 16 . Remainder = 8 & Quotient = 28
We further divide 28 by 16 and get remainder = 12 & quotient = 1
Now further dividing 1 by 16 results in remainder = 1 and quotient = 0
So we stop.
Now we take the remainders, bottom up.
1 , 12 , 8
Converting 12 in hex notation gives C.
So the answer is 1C8
To convert from decimal to hex you must know the powers of 16. 16^1 is obviously 16; 16^2 is 256; 16^3 is 4096; 16^4 is 65536; etc.
For each power of 16, divide the number by that power to get one hex digit. Then take the remainder and divide by the next lower power of 16.
This is enough of a hassle that it's easiest to let a calculator do it, or use a scripting language such as Python.
I would just like to confirm the following answer.
7037108.
8 does not form part of the number. Converting this to hexadecimal I get the answer ABCDE. Is this correct? If so what is the purpose of the subscript / base 8 which is written slightly below the number 703710.
A very simple way of converting octal to hex is through binary: octal digits correspond to three binary digits, while hex digits correspond to four. Convert the number to binary, regroup by four-bit nibbles, and then convert to hex.
The number converted to binary looks like this:
111 000 011 111 001 000
7 0 3 7 1 0
Regroup by nibbles and add leading zeros for conversion to hex:
0011 1000 0111 1100 1000
Now you can easily convert the number using a lookup table.
The subscript indicates what base the number is already written in, in this case 8, so it's octal.
So 7037108 is saying that the number is 7 * 8^5 + 0 * 8^4 + 3 * 8^3 + 7 * 8^2 + 1 * 8^1 + 0 * 8^0, or 231368 in base 10.
IOW, 7037108 = 23136810 = 387C816.
ABCDE (by which you mean ABCDE16, or 0xABCDE), would be the right answer if the original number were written in base 10, because 70371010 = ABCDE16.