#include <iostream>
int main()
{
unsigned int x{ 2 };
int y{-3};
std::cout<<x+y;
return 0;
}
This code is giving me the answer 4294967295.
#include <iostream>
int main()
{
unsigned short x{ 2 };
short y{-3};
std::cout<<x+y;
return 0;
}
When I change the int in previous code to short the new answer is -1.
As per my knowledge the correct answer in both cases should be 4294967295, This is because signed and unsigned integers arithmetic produce unsigned result but while using the short keyword the answer I get is different. But I don't understand why the code using short is giving the wrong answer, Can anyone explain what is going wrong here?
EDIT: This is NOT a duplicate of the other question. Stop flagging this ! Someone please unflag this
I have read What happens when I mix signed and unsigned types in C++? and it's helpful, but it doesn't address my question.
I am reposting this question because when I first asked this question someone flagged the question saying that it was duplicate but it was not so please dont flag this question