Nice reference to binary!by rpgfan3233 (no login)For those who don't understand how decimals convert to binary, here is one way to convert to binary: 1) Take a fraction, such as 1/10, and multiply it by 128. 2) This renders 12.8. 12 converted to binary is 1100. 3) This means that 1/10 is .110011001100... in binary. Another way to convert: 1) Take a fraction (1/10) and recursively multiply by 2, using the fractional part of the result as the new factor and the integer part as the digit after the decimal: 1/10 = 1/10 * 2 = 1/5, 1/5 < 1, so the first digit is 0 1/5 * 2 = 2/5, 2/5 < 1, so the next digit is 0 2/5 * 2 = 4/5, 4/5 < 1, so the next digit is 0 4/5 * 2 = 8/5, 8/5 >= 1, so the next digit is 1, 8/5 - 1 = 3/5 3/5 * 2 = 6/5, 6/5 >= 1, so the next digit is 1, 6/5 - 1 = 1/5 since you end up with 1/5 again and again, the pattern repeats starting at that point: 1/10 (decimal) = .0001100110011001100... (binary) A more complex example (5/8): 5/8 = 5/8 * 2 = 10/8 >= 1, so the first digit is 1, 10/8 - 1 = 1/4 1/4 * 2 = 1/2 < 1, so the next digit is 0 1/2 * 2 = 1 >= 1, so the next digit is 1, 1 - 1 = 0 0 * 2 = 0 < 1, so the next digit is 0 ^^^ = endless zeroes, but that is merely about how precise you want to be. 5/8 (decimal) = .1010000000000... (binary) OR 5/8 * 128 = 80.0. 80 converted to binary is 1010000 and the since the fractional part is 0, this means the number is .1010000000000... (binary) 5/8 (decimal) = .1010000000000... (binary) Both methods work. There are other methods, but why use up more forum space? :+) Negative numbers (twos complement) -2/5: use the absolute value first. 2/5 * 2 = 4/5 < 1, 0 4/5 * 2 = 8/5 >= 1, 1, 8/5 - 1 = 3/5 3/5 * 2 = 6/5 >= 1, 1, 6/5 - 1 = 1/5 1/5 * 2 = 2/5 < 1, 0 2/5 (decimal) = .0110011001100... (binary) Now for the negative part, use twos complement (invert the bits and add 1 to the last bit) -.01100110011001100... = .1001100110011...0011 + ...0001 = .10011001100110...100 (binary) |
| Response Title | Author and Date |
| place values | on Mar 23 |
Newbies usually go to www.qbasic.com and click on
The QBasic Forum
Forum regulars have their own ways, which include
The QBasic Community Forums