Key Takeaway
Before any arithmetic or bitwise operation runs, integer types smaller than int (such as bool, char, and short) are automatically converted into a value-preserving int (or unsigned int). This is known as integral promotion. As a result, true promotes to int 1, and flipping its bits with ~ yields -2 under two’s-complement representation; converting -2 back to bool evaluates to true because it is nonzero. To negate a boolean, always use ! instead of ~.
Have you ever reached for ~ to negate a bool, only to find the expression stubbornly evaluates to true? It is a subtle trap that catches many C++ developers off guard. This article explains what integral promotion is, why ~true evaluates back to true, and how to work safely with enums and bitmasks without running into unexpected conversions — backed by real compiler diagnostics.
What Is Integral Promotion? Why Do bool, char, and short Turn Into int?
One-line answer: It is the C++ standard rule that automatically converts integer types smaller than int into a value-preserving int (or unsigned int) before evaluating arithmetic or bitwise operations.

-
The underlying reason for integral promotion comes down to language rules: arithmetic operators (
+,-,*, etc.) and bitwise operators (~,|,&, etc.) in C++ do not accept operands smaller thanintdirectly. Therefore, right before the operation executes, prvalues ofchar,signed char,unsigned char,short,unsigned short,bool, and unscoped enums without a fixed underlying type are automatically promoted to a larger integer type. Ifintcan represent all values of the original type, the value promotes toint; otherwise, it promotes tounsigned int. The original value is always fully preserved (cppreference Implicit conversions). -
Promotion also plays a key role during overload resolution: an integral promotion such as
char -> inttakes precedence over a standard conversion likechar -> short. To inspect how the compiler performs these promotions behind the scenes, you can inspect the intermediate transformations directly on C++ Insights. -
We can verify with code that arithmetic on small integer types and bitwise operations on
uint8_tpromote toint. Usingstatic_assertandstd::is_same_v, we confirm at compile time that the types ofa + b(char + char),s + s(short + short),t + t(bool + bool), and~u(~std::uint8_t) are allint. On this test setup,std::uint8_tis an alias forunsigned char(implementation-defined), and the signedness of plaincharis likewise platform- and compiler-dependent.
// Check at compile time that char, short and bool arithmetic yields int
#include <cstdint>
#include <iostream>
#include <type_traits>
int main() {
char a = 100, b = 100;
short s = 1;
bool t = true;
std::uint8_t u = 0x0F;
auto c = a + b; // char + char
static_assert(std::is_same_v<decltype(c), int>);
static_assert(std::is_same_v<decltype(s + s), int>);
static_assert(std::is_same_v<decltype(t + t), int>);
static_assert(std::is_same_v<decltype(~u), int>); // uint8_t becomes int too
std::cout << "a + b = " << c << '\n';
std::cout << "true + true = " << (t + t) << '\n';
std::cout << "~u = " << ~u << '\n';
}
Compiler diagnostics (g++ 14.2.0 x86_64, -std=c++20 -Wall -Wextra): compiles cleanly with no warnings.
Output:
a + b = 200
true + true = 2
~u = -16
- Comparing signed and unsigned integers presents another classic trap caused by promotion rules. When comparing
int i = -1;againstunsigned char uc = 1;(i < uc),unsigned charhas a lower rank thanintandintcan represent its entire range, soucpromotes toint(1). The comparison becomes-1 < 1, which evaluates totrue. However, when comparingint i = -1;withunsigned int ui = 1;(i < ui),unsigned intis not promoted; instead, under the usual arithmetic conversions, the signediconverts tounsigned int. Unsigned conversion is modular, so-1becomes the maximum value ofunsigned int, and the comparison evaluates tofalse. This is not undefined behavior (UB) — it is a well-defined unsigned conversion under the C++ standard — but because it violates common intuition, you should always treat-Wsign-comparewarnings with care.
// Small unsigned types are promoted to int, but unsigned int is not
#include <iostream>
int main() {
int i = -1;
unsigned char uc = 1; // promoted to int -> -1 < 1 is true
unsigned int ui = 1; // i is converted to unsigned int -> compared as a huge value
std::cout << std::boolalpha;
std::cout << "-1 < (unsigned char)1 : " << (i < uc) << '\n';
std::cout << "-1 < (unsigned int)1 : " << (i < ui) << '\n';
}
Compiler diagnostics (g++ 14.2.0 x86_64, -std=c++20 -Wall -Wextra):
p4_sign_compare.en.cpp: In function ‘int main()’:
p4_sign_compare.en.cpp:11:51: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
11 | std::cout << "-1 < (unsigned int)1 : " << (i < ui) << '\n';
| ~~^~~~
Output:
-1 < (unsigned char)1 : true
-1 < (unsigned int)1 : false
Why Does ~true Evaluate to true Again?
One-line answer: true is promoted to int 1, bit-inverted into negative -2, and converted back to bool as true because any nonzero integer evaluates to true.

-
The evaluation of
~truetotrueproceeds in four distinct steps:- Integral promotion: The
boolvaluetruemeets the bitwise operator~and promotes to theintvalue1(false promotes to 0, true promotes to 1). - Bitwise inversion: A bitwise NOT operation (
~1) is performed onint 1. Since C++20 mandates two’s complement as the sole representation for signed integers (cppreference Fundamental types), inverting all bits yields exactly-2. - Boolean conversion: The resulting
int -2is converted back tobool. Because 0 converts tofalseand any nonzero value converts totrue,-2converts totrue. - Conclusion: Hence,
static_cast<bool>(~true) == trueholds true (Hacker News discussion mirror).
- Integral promotion: The
-
Applying a bitwise NOT to
false(~false) yieldstruethrough the identical mechanism.falsepromotes toint 0,~0evaluates to-1in two’s complement, and-1is nonzero, producingtruewhen converted back tobool. Thus, applying~to eithertrueorfalsealways yieldstrue, introducing a critical logical bug. -
To negate a boolean, you must always use the logical NOT operator (
!) instead of the bitwise NOT operator (~). With-Wall, the g++ 14.2.0 used here emits a-Wbool-operationwarning on~expressions withbooloperands, accompanied by the notedid you mean to use logical not (‘!’)?.
// Print step by step why ~true is true
#include <iostream>
bool Complement(bool x) {
return ~x; // meant as negation, actually an int bitwise NOT
}
int main() {
bool t = true;
int promoted = t; // promotion: true -> 1
int flipped = ~t; // ~1 -> -2 (two's complement guaranteed since C++20)
bool back = flipped; // nonzero, so true
std::cout << std::boolalpha;
std::cout << "promoted = " << promoted << '\n';
std::cout << "~true = " << flipped << '\n';
std::cout << "bool(~true) = " << back << '\n';
std::cout << "Complement(true) = " << Complement(true) << '\n';
std::cout << "Complement(false) = " << Complement(false) << '\n';
std::cout << "!true = " << !t << '\n';
}
Compiler diagnostics (g++ 14.2.0 x86_64, -std=c++20 -Wall -Wextra):
p2_not_true.en.cpp: In function ‘bool Complement(bool)’:
p2_not_true.en.cpp:5:13: warning: ‘~’ on an expression of type ‘bool’ [-Wbool-operation]
5 | return ~x; // meant as negation, actually an int bitwise NOT
| ^
p2_not_true.en.cpp:5:13: note: did you mean to use logical not (‘!’)?
p2_not_true.en.cpp: In function ‘int main()’:
p2_not_true.en.cpp:11:20: warning: ‘~’ on an expression of type ‘bool’ [-Wbool-operation]
11 | int flipped = ~t; // ~1 -> -2 (two's complement guaranteed since C++20)
| ^
p2_not_true.en.cpp:11:20: note: did you mean to use logical not (‘!’)?
Output:
promoted = 1
~true = -2
bool(~true) = true
Complement(true) = true
Complement(false) = true
!true = false
How Do You Avoid Enum and Bitmask Mistakes?
One-line answer: Define an enum class with an explicit fixed underlying type and overload its bitwise operators to prevent accidental int promotion and undefined behavior (UB).
-
An unscoped enum without a fixed underlying type (
enum Color { Red = 1, Green = 2 };) automatically promotes to the first integer type capable of holding its range — typicallyint— during bitwise operations such asRed | Green. When casting that computedintback to the enum viastatic_cast, if the value falls outside the enumeration’s valid range, the C++ standard defines it as undefined behavior (UB) (cppreference static_cast). -
Using a scoped
enum classwith a fixed underlying type eliminates this danger. Declaringenum class Perm : std::uint8_t { Read = 1, Write = 2, Exec = 4 };guarantees that casting an integer to this enum first converts through its underlying type, avoiding UB even if the value exceeds defined enumerator values (CWG 2338). By leveragingstd::underlying_type_tto overloadoperator|and ahasmembership test, you can implement type-safe bit flags. In C++23,std::to_underlyingfrom<utility>provides a concise way to retrieve an enum’s underlying integer value. Note that online discussions mention GCC displaying non-standard truncation behavior on bool-backed enums; because this is compiler-specific rather than standard behavior, we mention it solely as an external reference link (Hacker News discussion mirror).
// Handle bitmasks safely with enum class + a fixed underlying type
#include <cstdint>
#include <iostream>
#include <type_traits>
enum class Perm : std::uint8_t { Read = 1, Write = 2, Exec = 4 };
constexpr Perm operator|(Perm a, Perm b) {
using U = std::underlying_type_t<Perm>;
return static_cast<Perm>(static_cast<U>(a) | static_cast<U>(b));
}
constexpr bool has(Perm set, Perm p) {
using U = std::underlying_type_t<Perm>;
return (static_cast<U>(set) & static_cast<U>(p)) != 0;
}
enum Color { Red = 1, Green = 2 }; // unscoped, no fixed underlying type
int main() {
Perm rw = Perm::Read | Perm::Write;
std::cout << std::boolalpha;
std::cout << "rw has Write: " << has(rw, Perm::Write) << '\n';
std::cout << "rw has Exec: " << has(rw, Perm::Exec) << '\n';
auto x = Red | Green; // unscoped enums are promoted, so this is int arithmetic
static_assert(std::is_same_v<decltype(x), int>);
std::cout << "Red | Green = " << x << " (int)\n";
}
Compiler diagnostics (g++ 14.2.0 x86_64, -std=c++20 -Wall -Wextra): compiles cleanly with no warnings.
Output:
rw has Write: true
rw has Exec: false
Red | Green = 3 (int)
-
Here is a practical checklist for preventing integral promotion bugs:
- Boolean negation: Always use the logical NOT operator (
!) rather than the bitwise NOT operator (~) to negate abool. - Bitmask definitions: When implementing bit flags, use
enum classwith an explicit fixed underlying type (such asstd::uint8_t) instead of an unscoped enum, and overload the required bitwise operators. - Signed vs. unsigned comparisons: Remember that small unsigned types like
unsigned charpromote toint, whereasunsigned intdoes not; always keep implicit conversion rules in mind when comparing mixed signedness. - Enforce compiler warnings: Keep
-Wall -Wextraenabled to catch-Wbool-operationand-Wsign-comparewarnings during compilation.
- Boolean negation: Always use the logical NOT operator (
-
Integral promotion is an automatic conversion rule defined by the standard, so it doesn’t show up in your code. Where results can defy intuition, such as negating a bool, comparing mixed signedness, or doing bit operations on enums, it pays to know the rule and keep warnings on.
[Interview one-liner] Since C++‘s arithmetic and bitwise operators never take an operand smaller than int directly, they automatically promote it to int — so ~true goes through ~int(1) == -2, ends up nonzero, and evaluates back to true; always use !x for logical negation.
Interview answer template: “In C++, before an arithmetic or bitwise operation runs, small integer types like char, short, and bool undergo integral promotion into a value-preserving int (or unsigned int). So when you compute true, true is promoted to int 1, then bit-flipped into -2 (guaranteed two’s complement since C++20); converting that back to bool gives true because it’s nonzero. That’s why you should use logical NOT (!) instead of bitwise NOT () to negate a boolean, and why building bitmasks should use enum class with a fixed underlying type to avoid unintended promotion side effects.”
Shall We Check Your Understanding With a Q&A?
Q1. What is integral promotion in C++, and why does it happen?
A1. It is the C++ standard rule that automatically converts small integer types like bool, char, and short, as well as unscoped enums without a fixed underlying type, into a value-preserving int (or unsigned int) right before an operation runs — because arithmetic and bitwise operators do not directly accept operands smaller than int.
Q2. Why does static_cast<bool>(~true) evaluate back to true?
A2. Because true is promoted by integral promotion into int 1, then bit-flipped (~) into -2 (two’s complement is guaranteed since C++20), and that -2, being nonzero, converts to true according to boolean conversion rules.
Q3. Given int i = -1; unsigned char uc = 1; unsigned int ui = 1;, why is i < uc true while i < ui is false?
A3. unsigned char has a lower rank than int, and int can hold its entire range, so uc is promoted to int(1), making the comparison -1 < 1, which is true. unsigned int, on the other hand, is not a promotion target, so under the usual arithmetic conversions int -1 is converted to the maximum value of unsigned int before comparison, making it false.
Q4. Why use enum class with a fixed underlying type instead of a plain unscoped enum for bitmasks?
A4. A plain unscoped enum promotes to int during operations, causing unintended type mixing, and casting the result back to the enum can trigger undefined behavior (UB) if the value falls outside its range. An enum class with a fixed underlying type converts through that underlying type first, so there is no UB (CWG 2338) and you obtain strong type safety.