How to convert signed integers from decimal system to signed binary in two's complement representation
Follow the steps below to convert a signed base 10 integer number to signed binary in two's complement representation:
- 1. If the number to be converted is negative, start with the positive version of the number.
- 2. Divide repeatedly by 2 the positive representation of the integer number, keeping track of each remainder, until we get a quotient that is zero.
- 3. Construct the base 2 representation of the positive number, by taking all the remainders starting from the bottom of the list constructed above. Thus, the last remainder of the divisions becomes the first symbol (the leftmost) of the base two number, while the first remainder becomes the last symbol (the rightmost).
- 4. Binary numbers represented in computer language must have 4, 8, 16, 32, 64, ... bit length (a power of 2) - if needed, add extra bits on 0 in front (to the left) of the base 2 number above, up to the required length, so that the first bit (the leftmost) will be 0, correctly representing a positive number.
- 5. To get the negative integer number representation in signed binary one's complement, replace all 0 bits with 1s and all 1 bits with 0s (reversing the digits).
- 6. To get the negative integer number, in signed binary two's complement representation, add 1 to the number above.
Example: convert the negative number -60 from the decimal system (base ten) to signed binary in two's complement:
- 1. Start with the positive version of the number: |-60| = 60
- 2. Divide repeatedly 60 by 2, keeping track of each remainder:
- division = quotient + remainder
- 60 ÷ 2 = 30 + 0
- 30 ÷ 2 = 15 + 0
- 15 ÷ 2 = 7 + 1
- 7 ÷ 2 = 3 + 1
- 3 ÷ 2 = 1 + 1
- 1 ÷ 2 = 0 + 1
- 3. Construct the base 2 representation of the positive number, by taking all the remainders starting from the bottom of the list constructed above:
60(10) = 11 1100(2) - 4. Bit length of base 2 representation number is 6, so the positive binary computer representation of a signed binary will take in this particular case 8 bits (the least power of 2 larger than 6) - add extra 0 digits in front of the base 2 number, up to the required length:
60(10) = 0011 1100(2) - 5. To get the negative integer number representation in signed binary one's complement, replace all the 0 bits with 1s and all 1 bits with 0s (reversing the digits):
!(0011 1100) = 1100 0011 - 6. To get the negative integer number, signed binary in two's complement representation, add 1 to the number above:
-60(10) = 1100 0011 + 1 = 1100 0100 - Number -60(10), signed integer, converted from decimal system (base 10) to signed binary two's complement representation = 1100 0100