- 1). Convert a decimal number to a binary number by repeatedly dividing the decimal number by 2 and keeping track of the remainders. For example, to convert decimal 50 to binary, start by dividing by 2 into 50, and keep dividing 2 into the quotients until the 50 is gone, keeping track of the remainders. 50/2 = 25 with remainder of 0. 25/2 = 12 with remainder of 1. 12/2 = 6 with remainder 0. 6/2 = 3 with remainder 0. 3/2 = 1 with remainder 1. 1/2 = 0 with remainder 1. The remainders --from last to first -- are 110010, but if we are using an 8-bit registers all strings have 8 digits so pad with zeros to the left to get 0011 0010.
- 2). Use "two's complement" notation to represent negative numbers. To convert a binary number into a negative number, flip all the bits and add 1. For example, -50 is 0011 0010 converted to two's complement or 1100 1101 + 1 = 1100 1110. One of the good things about representing negative numbers as two's complements is that the leftmost digit in the string lets you know if the string represents a positive or negative number. If the leftmost bit is zero, the number is positive. If the leftmost bit is one, the number is negative.
- 3). Know the limits imposed by the register size and formats. If you are using an 8-bit register and are using the leftmost bit to represent the sign, you have only have 7 positions to represent magnitude. Seven bits can represent the distinct numbers from 0 to 2^7 - 1 or 0 to 127 and the negative numbers down to -127. If you need to represent numbers outside of this range, you need a bigger register.
previous post