views:

59

answers:

5

I am reading a book on assembly languages. I came across these sentences in that book.

Consider the value “-64”. The eight bit two’s complement value for this number is
0C0h. The 16-bit equivalent of this number is 0FFC0h.

I can't understand these two sentences. Can anybody show me how -64's eight bit 2's complement is 0c0h? And how the 16 bit equivalent is 0ffc0h? Please show me the calculation if possible. Thanks in advance.

+1  A: 

It may help to picture a negative number as the subtraction of two positive numbers, such as "0 - 64".

Zero is just 00h in 8 bits.

Subtract 1 and you'll get FFh.

Subtract 1 again and you'll get FEh.

Continue that pattern 62 more times and you'll be at C0h.

Try the pattern again in 16 bits, remembering that subracting one from 0000h is FFFFh.

Shmoopty
+1  A: 

+64 = %0100_0000
-64 = %1011_1111 + 1 = %1100_0000 = 0xC0
When you take an 8 bit signed 2's complement number and extend it to 16 bits, you must sign extend it (copy the sign bit into all of the new HO bits)

This makes -64 = %1111_1111_1100_0000 = 0xFFC0

RD
+2  A: 

Integers in a CPU are represented using a fixed number of binary digits. For example, 64 expressed in binary using eight bits is 01000000.

Generally, negative numbers are expressed in two's complement. To get the two's complement of a binary number, you first compute the one's complement of the positive number (which means, flip all the bits), then add one.

For example, to calculate the two's complement of -64, start with 64 in binary:

01000000 then flip all the bits to get one's complement
10111111 then add one, ignoring the final carry (i.e. overflow)
11000000

11000000 is C0 in hex.

The same process can be carried out using 16-bits:

00000000 01000000 (64)
11111111 10111111 (one's complement of 64)
11111111 11000000 (one's complement of 64 plus one)

11111111 11000000 in hex is FFC0.

The reason two's complement is used for negative numbers is because it eliminates special cases. A negative number and a positive number can be added together normally, and the right answer will result. For example, -1 in 8-bit two's complement is 11111111. Adding one to that correctly gives back zero (11111111 + 00000001 = 00000000), since there are not enough bits to hold the carry.

Cameron
A: 

I'm going to use subscripts for bases rather than any programming language's syntax. First off, in positive numbers,

+6410 = +4016

The two's complement representation of a negative number is the bitwise complement of that number, plus 1. In 8 bits,

-(4016) = ~(4016) + 1 = BF16 + 1 = C016

as your book says. To get the 16-bit representation, you copy the highest bit of the 8-bit representation to all higher bits (this is called sign extension):

4016 = 004016
C016 = FFC016

You have to know from context whether sign extension or zero extension is appropriate. C016, without context, is ambiguous: it could be either -6410 or +19210. If the latter, it should be zero-extended to 00C016 rather than sign-extended to FFC016.

Zack
A: 

Imagine a machine which used an infinite number of bits for integers. Suppose one is subtracted from a number whose rightmost 'k' bits are all zero, and whose next bit is '1'. The rightmost 'k' zeroes will all become ones, and the next bit will become a zero.

Now suppose that for some 'k', the rightmost 'k' bits are zeroes and the next bit is unknown, but there's known to be a '1' somewhere. In that scenario, even without knowing the upper bits, one still knows that the bottom 'k' bits should go from zero to '1'.

Two's complement arithmetic is based on the premise that subtracting one from anything should convert all trailing zeroes to '1's, without a special case for the number being zero. If a number in binary contains an infinite number of trailing zeroes, subtracting one should yield an infinite number of '1's.

Incidentally, note that the common formula for a geometric series works here: the sumk=0..inf is 1/(1-a). Normally used with 'a' values like 1/2 (e.g. the sum of 1 + 1/2 + 1/4 + 1/8 etc. equals 2) it 'works' for a value of 2 (yielding a total equal -1).

Of course, it's not possible for computers to store an infinite number of '1's. Therefore, there's a convention which says that the highest bit in the stored representation of a number actually represents the state of all bits beyond.

supercat