223k views
0 votes
How to you convert (both positive and negative integers) denary to Two’s complement and vice versa?

1 Answer

4 votes

Answer:

To convert from decimal to binary, one approach is to repeatedly divide by 2 as integer division and write down the remainders from right to left:

example: convert 26 to binary

26 / 2 = 13, no remainder => write down 0

13 / 2 = 6, remainder 1 => write down 1

6 / 2 = 3, no remainder => write down 0

3 / 2 = 1, remainder 1 => write down 1

1 / 2 = 0, remainder 1 => write down 1

So 11010 is your result.

For 2's complement, you have to consider youre entire word size. Let's say you have 8 bit representations, then 11010 is really 00011010.

To get the 2's complement, you invert the binary represenation and add 1:

00011010 => 11100101

11100101 + 1 = 11100110 (understand binary addition for this)

So 11100110 is the binary representation of -26.

You can do this entire sequence in reverse, i.e. subtract one, invert and then go back to the decimal representation:

11010 in decimal is 1·2⁴ + 1·2³+ 0·2²+ 1·2¹+ 0·2⁰ = 26

User Xareyo
by
4.2k points