SciForums.com > Technology > Computer Science & Culture > Conversion between base 10 and base 2 PDA View Full Version : Conversion between base 10 and base 2 Post ReplyCreate New Thread cfso195211-06-04, 09:03 PMHow to convert from base 2 to 10 or 10 to 2???????? Including conversion of integers, decimals How to carry out mathematical operations in base 2???????????????? geodesic11-06-04, 10:22 PMTo convert from base 10 to base 2: find out how many digits you need by taking log base 2 of your number, which we shall call x, rounded down plus 1. Take n equal to the number of digits. Set each digit (starting from the left) by doing integer division of number x, and then set x as the remainder. decrement n and repeat the previous step until n=0. eg. 168 has 8 digits, 10101000 To go from 2 to 10: Multiply each digit by 2 to the power of of its distance from the right minus 1. eg. 1001101=1x1+1x4+1x8+1x64=77. Why do you ask? Voodoo Child11-07-04, 04:21 AMdecimals depends upon the format of the number. As I recall the number is normalised in the form of .101110101(there is an implicit 1 at the start that need not be recorded). The exponent is used to tell how far to move the "decimal" point. There is also a sign bit. The IEEE convention is: 1 bit sign /8 bit exponent/23 bit mantissa: 0 11111111 0101010 10101010 10101010 the exponent(unsigned) is converted to decimal, and then has 127 subtracted from it. if -ve, the dp goes <---. If +ve the dp goes --->. So the mantissa, 11111111. (or 27 after the bias is removed) means the dp goes 7 to the right. That is your floating point. To convert a binary no to base human: eg. 101010.101 is merely 25 * 1 + 24 *0+ 23 *1 + 22 *0 + 21 *1 + 20 *0 + 2-1 *1 + 2-2 *0 + 2-3 *1 does that make any sense at all? geodesic11-07-04, 12:14 PMSurely the exponent is the power of two to which the number is raised? Your exponent (which you have mis-named the mantissa) is 128 in decimal, which means the dp. moves 128 places right. You're multiplying by the exponent. And isn't the exponent in twos complement form, where the msb is negative, and all the others are positive? That would make your exponent -1. Voodoo Child11-07-04, 11:38 PMYour exponent (which you have mis-named the mantissa) is 128 in decimal, which means the dp. moves 128 places right. True. Should read: "(or 27 after the bias is removed) means the dp goes 27 to the right. ". Which is also wrong(should be 27-1) And isn't the exponent in twos complement form, where the msb is negative, and all the others are positive? No, it's biased. The reason is that 2's comp would make comparisons too hard. from http://www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html The exponent, which is a signed integer in the range from -126 to 127, is represented neither as a signed magnitude nor as a twos-complement number, but as a biased value. The idea here is that the integers in the desired range of exponents are first adjusted by adding a fixed ``bias'' to each one. The bias is chosen to be large enough to convert every integer in the range into a positive integer, which is then stored as a binary numeral. The IEEE single-precision representation uses a bias of 127. edit: was also incorrect in that the bias is added and not subtracted. Post ReplyCreate New Thread