+ 17

C++ challenge question from my declined list: Guess the output

Another interesting question. No code playground! Try it for yourselves. Brace yourselves: What is the output of the following code? Assume 32 bit integers. cout << 25u - 50; a. -25 b. 25 c. 4294967271 d. Error Best answer to whoever gets the correct answer and explains why the answer is correct.

24th May 2017, 4:32 AM
Ace
Ace - avatar
14 Answers
+ 26
c. 4294967271 In C++, if the datatypes of two values used in calculation is different from each other, the value with datatype of lower size hierarchy will be converted to the datatype of the other value which has the higher size hierarchy. Size hierarchy : short int < unsigned short int < int < unsigned int < long int < unsigned long int Size hierarchy for floating point numbers is : float < double < long double In the question, 25u is an unsigned int. 50 is an int. So according to the above information, 50 will be converted from int to unsigned int. Hence, 25u - 50u is computed and the result of 4294967271 is obtained. // I'm not sure how the conversion works though. What I am sure of was that the answer would neither be a, b or d. // would have needed this: https://www.quora.com/How-do-I-convert-an-integer-into-an-unsigned-integer-in-C
24th May 2017, 4:54 AM
Hatsy Rei
Hatsy Rei - avatar
+ 14
Well done @Hatsy Rei. The key to understanding this question was the u means unsigned. In C++, if the types of two operands differ from one another, then the operand with the "lower type" will be promoted to the type of the "higher type" operands​. unsigned has a higher order than signed thus the 50 becomes 50u. Moreover, the result of the operation will be of the type of the operands. Therefore, the result of 25u - 50u will itself be an unsigned integer as well. So the result of -25 converts to 4294967271 when promoted to being an unsigned integer.
24th May 2017, 5:01 AM
Ace
Ace - avatar
+ 10
This was covered in the c++ course btw, just not in great detail. I was asked this question before my very first internship quite awhile ago, and I worked it out in this manner.
24th May 2017, 5:04 AM
Ace
Ace - avatar
+ 7
If you know the answer and can explain why, share mate 👍
24th May 2017, 4:52 AM
Ace
Ace - avatar
+ 6
Any particular reason for picking a @Sumita?
24th May 2017, 4:53 AM
Ace
Ace - avatar
+ 5
@Hatsy rei I got a headache just reading that
24th May 2017, 4:53 AM
DeleteThisAccount
+ 4
Crap. I know the answer but I can't share. No, I didn't use CP, but I've ran a code like this before. I'll give a hint though, the u defines what type of integer 25 is and this is important to the Mathematical operation being performed.
24th May 2017, 4:42 AM
Steven Schneider
Steven Schneider - avatar
+ 4
@Hasty Rei is correct though.
24th May 2017, 4:54 AM
Steven Schneider
Steven Schneider - avatar
+ 4
*mindblown* Nice question, I wish that was excepted. I am going have to review that.
24th May 2017, 5:06 AM
Manual
Manual - avatar
+ 3
what is u here ,is it for unsigned
24th May 2017, 4:41 AM
Sumita
Sumita - avatar
+ 3
@Sumita Will the final answer be signed or unsigned if one integer is an unsigned?
24th May 2017, 4:52 AM
Steven Schneider
Steven Schneider - avatar
+ 3
@Ace I bet it was downvoted because people did not get it. It is a kata level question I like it. If I came accross it. I would skip it, until I compiled it.
24th May 2017, 4:58 AM
Manual
Manual - avatar
24th May 2017, 4:54 AM
Manual
Manual - avatar
+ 1
Wow @Hatsy what can you do with the data type hierarchy?
24th May 2017, 5:00 AM
Manual
Manual - avatar