tags:

views:

149

answers:

6

What is the result of adding the binary numbers 01000001 and 11111111 on an 8 bit machine?

+1  A: 

Integer overflow.

If the numbers are unsigned (i.e. modular), 0100000 (with modular 8-bit math, addition of 11111111 is equal to subtraction of 1).

Williham Totland
Not if they are two's complement
Paul R
@Paul: In two's complement it also results in overflow. Most computers just silently truncate the result.
Bart van Ingen Schenau
@Bart: in 2's complement 01000001 = 65 and 11111111 = -1. Adding these gives 01000000 = 64. There is no overflow in this case, i.e. the V flag does not get set on a typical CPU.
Paul R
@Paul: You are right. I don't know what I was thinking, must have been a brain-fart. In fact, it also does not overflow with signed-magnitude (65 + -127 = -62) or one's complement (assuming 11111111 is not a trap representation; 65 + -0 = 65).
Bart van Ingen Schenau
A: 

I think you just add the numbers, then cut the overflowing bits (from the left)
EDIT: I can't post comments yet, so I will ask here: I always thought that binary comlement was designed so that when adding or substracting, you don't have to care about it. Am I wrong?

Dadam
A: 
  • If these are signed integers, they represent 65 and -128 -1. Adding them will give -63 64.
  • If these are unsigned integers, they represent 65 and 255. Since the sum can not be represented in 8 bits, the result will be 64 and the overflow bit will be set.
Sjoerd
If they are signed, they are 65 and -1.
mikera
Uh... how do you come up with -128? Assuming two's complement (which is the most common representation and most likely what the question assumes), 11111111 is -1... and the sum will be 01000000, or 64.
Martin B
For the unsigned case you are on the wrong track, since the standard requires that unsigned arithmetic wraps silently. There is no such thing as an overflow bit.
Jens Gustedt
Most machines have an overflow bit, but it is not available in C.
Sjoerd
+1  A: 

If we are supposed to interpret this with the rules of C (it is tagged as such), for the signed case there are three interpretations of these numbers possible, corresponding to the three sign representations that are allowed in C.

For the unsigned case the standard requires that unsigned arithmetic wraps silently. All computation is done modulo 256 in that case.

Jens Gustedt
A: 

If both values are unsigned, then the result is 320 in decimal. Both operands are promoted to int before the addition, and int is required by the standard to have at least 16 bits, even on an 8 bit machine. The question doesn't make any restrictions for the result.

Secure
A: 

Unless you want the "wrong result fast", the answer is 320.

Correctly adding two numbers (in whatever representation) anywhere (including 8-bit machines) results in a unique number that can be represented in a multitude of different ways.

320 can be represented as 320 (usual decimal (base-10) representation) or 101000000 (binary representation) or 253413120100 (factoradic), ...

pmg