-2

How int overflow works. I mean to ask what would be the final result of an integer value if an overflow happens? I need to understand it on paper. Like I'm given a multiple choice question:

11^5 Produces:
a. 12
b. 14
c. 15
d. 17.

I know the answer is (b)14, but want to know why?

7
  • 3
    Upvoting for I think it's good that people seek explanations of things. Commented May 20, 2015 at 21:06
  • Most architectures use a single bit for the sign, and fill up with 1's. Commented May 20, 2015 at 21:06
  • 1
    Have a look here Commented May 20, 2015 at 21:08
  • 1
    MCQ? There's far too many TLAs these days... Commented May 20, 2015 at 21:09
  • 2
    11 ^ 5 --> 14 (decimal) --> 1011 xor 0101 --> 1110 (binary). No overflow involved. Commented May 20, 2015 at 21:32

2 Answers 2

5
   DecVal   BinVal
    11   -> 1011
XOR 5    -> 0101
           -------
    14   -> 1110

fyi, This does not really have anything to do with int overflow.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes here goes the answer. Thanks Sir you got my problem solved.
@MohsinAli And how this is an overflow problem like you insisted several times?
look sir I'm new to all these, I thought if it nothing else then it would be overflow problem.
@MohsinAli It´s a XOR ([bitwise] exclusive "or"), a kind of bitwise operation. Essentially a calculation like plus and minus, just different from plus and minus.
@deviantfan I really confused you sir, and I apologize for that.
|
1

Since you're asking about wrap-around I'm answering that below. But as is clear from comments, you really tried the C++ expression 11^5, a bitlevel XOR, and got the answer 14, which has nothing to do with wraparound. The XOR result of each pair of bits is 0 if they're the same value, and 1 if they're different.


Now, in the following ^ denotes exponentiation; it's a common notation for that.

11^5 = 161051.

Now consider a situation where 16 bits are used to represent integers, and there's only magnitude, no sign. I.e. a 16-bit unsigned C++ integer type. Then there are 2^16 possible bit patterns, and they are numbered 0 through 2^16 - 1, and represent those numbers.

161051 is larger than the largest possible value of that 16-bit type. If it were 2^16 exactly it would correspond to 0 (called wrap-around), if it were 2^16 + 1 it would correspond to 1, and so on. So it corresponds to 161051 - 2^16.

Now if that in turn also was greater than 2^16 - 1 you would repeat the process.

And this produces the remainder of integer division by 2^16.

Essentially it correspond to just removing all the bits except the 16 least significant ones.

By the way the result for this example is not any of your choices (a), (b), (c) or (d).

14 Comments

Appreciate your concern sir, But I run the program on c++ compiler it showed up being (b) 14.
@Moshin: Aha. You used the expression 11^5 in C++ source code. That does not calculate a power and wrap around. It is a bitlevel XOR operation.
Sir I need to understand that, Kindly if you could impart the knowledge.
@MohsinAli Just as side note, the result 14 (with the correct operations) isn´t possible at all, independent from the int size.
@deviantfan how would it be possible sir?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.