Why does the C++ comparison -1 < 1u evaluate to false, and which rule picks the common type?
answer
- both operands meet in one type
- promote first, then compare ranks
- same rank: the unsigned type wins
- -1 becomes UINT_MAX
- long versus unsigned int depends on width
basics
~20 sThe usual arithmetic conversions convert both operands to one common type. int and unsigned int share a rank, so the int converts to unsigned and -1 becomes UINT_MAX (4294967295 for 32 bits), not less than 1.
solid answer
~40 sBinary arithmetic and comparison operators apply the **usual arithmetic conversions**: integral promotion first, then a common type. If both types are signed or both unsigned, the higher rank wins. If they differ in signedness and the unsigned type's rank is at least the signed one's, everything becomes unsigned — so in `-1 < 1u` the `int` converts to `unsigned int`, -1 becomes `UINT_MAX`, and the comparison is false. If the signed type has the higher rank and can represent every value of the unsigned one, the signed type wins: `-1L < 1u` is true on LP64, where `long` is 64 bits, but false on 64-bit Windows, where `long` is 32 bits and the result type is `unsigned long`. Compilers warn with `-Wsign-compare` (GCC's `-Wall` in C++, Clang's `-Wextra`).
code
cpp · 14 lines#include <iostream>
#include <type_traits>
int main() {
int delta = -1;
unsigned int limit = 1;
std::cout << std::boolalpha;
std::cout << (delta < limit) << '\n'; // int converts to unsigned int
std::cout << (delta + limit) << '\n'; // 0: the sum wraps modulo 2^32
std::cout << (delta < 1L) << '\n'; // both signed: long
static_assert(std::is_same_v<decltype(delta + limit), unsigned int>);
static_assert(std::is_same_v<decltype(delta + 1L), long>);
std::cout << static_cast<unsigned int>(delta) << '\n';
}go deeper
Remember that mixing signed and unsigned operands converts the signed one, so a negative value turns into a huge positive one.
Walk through promotion, rank and the signed-versus-unsigned branch, and show why -1L < 1u differs between LP64 and LLP64.
Explain which warnings catch mixed-sign comparisons and how std::cmp_less or an explicit, checked cast fixes them at an API boundary.
Treat mixed-sign arithmetic as a policy question: which warnings are errors in the build, and where conversions are allowed to happen.
## One expression, one common type Most binary operators on arithmetic operands — `+`, `-`, `*`, `/`, `%`, the relational and equality operators, the bitwise `&`, `|`, `^` — first bring both operands to a single **common type**, and that is also the type of an arithmetic result. The standard calls this the **usual arithmetic conversions**. They run silently, before the operator does anything, and they are where most "impossible" comparisons come from. ## The algorithm for integers For two integer operands the steps are: 1. Apply **integral promotion** to each operand: anything ranked below `int` (`bool`, `char`, `short`, the small `<cstdint>` types) becomes `int`, or `unsigned int` if `int` cannot hold all its values. 2. If the two promoted types are the same, that is the common type. 3. If both are signed or both are unsigned, the type with the greater **integer conversion rank** wins (`long long` > `long` > `int`). 4. Otherwise one is signed and one unsigned: - if the unsigned type's rank is **greater than or equal to** the signed type's rank, the common type is the unsigned type; - otherwise, if the signed type can represent **every** value of the unsigned type, the common type is the signed type; - otherwise, it is the unsigned type corresponding to the signed type. Converting a negative value to an unsigned type yields the value congruent modulo 2^N, so -1 becomes the largest value of that type. Step 1 matters more than it looks. Because promotion happens first, `bool`, `char` and `short` operands never reach the signed-versus-unsigned branch as themselves: `'a' + 1` is an `int`, and two `unsigned short` values compare as `int` on every mainstream platform. The sign trap only springs once one operand is already `unsigned int` or wider — which is exactly what `std::size_t`, `.size()` and unsigned literals such as `1u` are. ## Worked examples | Expression | Common type (64-bit Linux) | Result | |---|---|---| | `-1 < 1u` | `unsigned int` | false: -1 becomes 4294967295 | | `-1 < 1L` | `long` | true: both signed | | `-1 < 1ul` | `unsigned long` | false | | `-1L < 1u` | `long` (LP64) | true | | `-1L < 1u` | `unsigned long` (64-bit Windows) | false | The last two rows are the same source line with different answers. On LP64, `long` is 64 bits and holds every `unsigned int` value, so rule 4b picks `long`. On 64-bit Windows `long` is 32 bits, cannot hold `UINT_MAX`, and rule 4c picks `unsigned long`. A comparison that passes code review on Linux can therefore flip on another platform without any source change. ## Floating operands If either operand is floating, the other is converted to that floating type; between two floating types the one with the lower rank converts to the higher (`float` to `double`). Integers never pull a floating operand down: `3 / 2.0` is `1.5` because the `3` becomes a `double`. ## Where it bites - `if (offset < buffer.size())` with a negative `int` offset: `size()` returns `std::size_t`, so the offset converts to a huge unsigned value and the test is false — or true, when the comparison is the other way round. - `long long balance = deposits - withdrawals;` with `unsigned int` operands: the subtraction wraps in `unsigned int` first, so 3 - 5 stores 4294967294, not -2. - `for (int i = 0; i < v.size() - 1; ++i)` on an empty vector: `v.size() - 1` wraps to the maximum `std::size_t`. ## Diagnosing and fixing - **Warnings**: `-Wsign-compare` flags mixed-sign comparisons; GCC enables it with `-Wall` for C++, Clang with `-Wextra`. Clang 21 printed nothing for the all-literal `-1 < 1u`, so do not rely on it for constants. `-Wsign-conversion` reports every implicit sign change and is noisy but thorough. - **C++20 comparison functions**: `std::cmp_less(a, b)` and its siblings in `<utility>` compare the mathematical values of two integers of any signedness, so `std::cmp_less(-1, 1u)` is true. - **Make the conversion explicit** at the boundary, after checking the value: `if (offset >= 0 && static_cast<std::size_t>(offset) < buffer.size())`. The key interview point is that nothing here is undefined behaviour: the standard fully specifies the result, and the result is simply not the one the reader expected.
- Why can the same comparison of a long with an unsigned int give different results on Linux and Windows?The rule depends on whether the signed type can represent every value of the unsigned type. On LP64 Linux, a 64-bit `long` holds all 32-bit `unsigned int` values, so `-1L < 1u` compares as `long` and is true. On 64-bit Windows `long` is 32 bits, so both convert to `unsigned long` and the result is false.
- How does C++20's std::cmp_less avoid the conversion trap?`std::cmp_less(t, u)` in `<utility>` compares mathematical values: when exactly one argument is signed and negative, it returns the answer directly instead of converting, and otherwise compares after a safe conversion. It accepts only signed and unsigned integer types, so passing `char` or `bool` does not compile.
- the compiler converts both operands to the wider signed type when signs differ
- comparing a negative int with an unsigned value is undefined behaviour
- -1 < 1u is true because minus one is less than one
- a cast to int on the unsigned side is always a safe fix
- the conversion rules are the same on every 64-bit platform