C++ integer types can be signed or unsigned.
The difference sets whether or not negative values are permitted and how you go about using the bits available.
Signed integer can hold both negative and positive values.
This will be the default behavior for the simple integer types.
#include <iostream>
int main()
{
int temperature{-5};
int score{42};
std::cout << "Temperature: " << temperature << '\n';
std::cout << "Score: " << score << '\n';
}
You will usually just write int.
The compiler always considers this as signed, unless you make a request for another option.
Unsigned int can represent only zero or positive numbers.
This uses the same amount of memory as the signed version, but as all the bits are used to represent positive numbers, the positive range is greater..
#include <iostream>
int main()
{
unsigned int count{0};
count = 10;
std::cout << "Count: " << count << '\n';
}
Unsigned types may occasionally be used for quantities, such as sizes and counts, that logically cannot be negative.
In application, they need additional attention:
Mixing signed and unsigned in the same expression usually results in surprise.
#include <iostream>
int main()
{
int signed_value{-1};
unsigned int unsigned_value{1};
if (signed_value < unsigned_value)
{
std::cout << "Signed is smaller\n";
}
else
{
std::cout << "Unsigned wins the comparison\n";
}
}
The negative signed value may be converted to a large unsigned number just prior to the comparison.
Therefore, the condition can give a mathematically wrong answer.
This little silent conversion is a common source of insidious bugs.
Exercise: Signed and Unsigned TypesStarter code, hints and solutionOpenClose
Practice: Signed and Unsigned Types
Work through the mission, reveal hints only when needed, and compare your result with the model solution.
Your challenge
Change signed_value from -1 to 0 while keeping unsigned_value at 1. Predict the comparison branch and explain the conversion involved.
Mission checklist
- Run the starter as a baseline, then predict the change requested in Your challenge.
- Make that change in your editor; use the lesson-specific hints if you need help.
- Compile and run your answer, then compare with the worked solution and explain the result.
Starter program
This is the working baseline, not the completed answer. Compile as C++20, then make the change described above.
#include <iostream>
int main()
{
int signed_value{-1};
unsigned int unsigned_value{1};
if (signed_value < unsigned_value)
{
std::cout << "Signed is smaller\n";
}
else
{
std::cout << "Unsigned wins the comparison\n";
}
}Hints
The comparison mixes int and unsigned int. Before comparing these particular types, signed_value is converted to unsigned int, so do not compare -1 and 1 as ordinary signed numbers.
Try signed_value equal to 0, then compare the chosen branch with the -1 case. Keep unsigned_value unchanged to isolate the effect of the conversion.
Worked solution
signed_value is now 0. When compared with unsigned_value, it converts to unsigned zero, which is less than one, so the Signed is smaller branch runs. This differs from converting the original -1 to unsigned.
#include <iostream>
int main()
{
int signed_value{0};
unsigned int unsigned_value{1};
if (signed_value < unsigned_value)
{
std::cout << "Signed is smaller\n";
}
else
{
std::cout << "Unsigned wins the comparison\n";
}
}
Example output
Signed is smaller
0 comments
No comments yet. Be the first.