Unsigned/Signed
-
So a value declared int in C++. Is signed. However if I choose unsigned and assigned a negative value. It should be zero. Instead I get a large value, based on the most significant bit being 1 (representing the sign value). And whatever was the original value being assigned is deducted from that value. So -0, if nothing is to be deducted. Someone should it be doing this?
-
So a value declared int in C++. Is signed. However if I choose unsigned and assigned a negative value. It should be zero. Instead I get a large value, based on the most significant bit being 1 (representing the sign value). And whatever was the original value being assigned is deducted from that value. So -0, if nothing is to be deducted. Someone should it be doing this?
C++ does no checking at runtime when doing automatic conversions. The bits from the signed int are simply crammed into the unsigned int. A negative integer has the high bit set so in the unsigned world it looks like a large number. If you set the warning level to 4, a good idea in general, with VC++ you'll get a signed/unsigned mismatch warning at compile time. You should look at all warnings to make sure you're not shooting yourself in the foot accidentally and fix them with proper casts, etc, so your compiles are totally clean. That will save you a lot of work in the long run looking for subtle bugs like the wrap around you're mentioning.
Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.
-
C++ does no checking at runtime when doing automatic conversions. The bits from the signed int are simply crammed into the unsigned int. A negative integer has the high bit set so in the unsigned world it looks like a large number. If you set the warning level to 4, a good idea in general, with VC++ you'll get a signed/unsigned mismatch warning at compile time. You should look at all warnings to make sure you're not shooting yourself in the foot accidentally and fix them with proper casts, etc, so your compiles are totally clean. That will save you a lot of work in the long run looking for subtle bugs like the wrap around you're mentioning.
Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.
Yes, it should most definitely be doing this! What you are assigning to a variable is a bit pattern - not a value. Whether the variable is signed or not is a question of interpretation. Your interpretation! Assigning a negative constant to an unsigned variable is perfectly legitimate. As Tim Craig pointed out, you should trust your beloved and most obedient servant - the compiler. All compilers will warn you about signed/unsigned mismatch. - turin
-
So a value declared int in C++. Is signed. However if I choose unsigned and assigned a negative value. It should be zero. Instead I get a large value, based on the most significant bit being 1 (representing the sign value). And whatever was the original value being assigned is deducted from that value. So -0, if nothing is to be deducted. Someone should it be doing this?
Fareed Rizkalla wrote:
It should be zero.
this requirement exists only in your mind. C/C++ does not work that way
-
Yes, it should most definitely be doing this! What you are assigning to a variable is a bit pattern - not a value. Whether the variable is signed or not is a question of interpretation. Your interpretation! Assigning a negative constant to an unsigned variable is perfectly legitimate. As Tim Craig pointed out, you should trust your beloved and most obedient servant - the compiler. All compilers will warn you about signed/unsigned mismatch. - turin
Just FYI, if you want to be sure that the author gets this message, reply to the author's post and you can refer to Craig's post, as you did in the text. The author didn't get the email, Tim Craig did.
Josh Davis
This is what plays in my head when I finish projects.