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.

bool, char, short, uint8_t and unscoped enums are promoted to int (or unsigned int) before arithmetic and bit operations; unsigned int is not promoted

  1. 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 than int directly. Therefore, right before the operation executes, prvalues of char, signed char, unsigned char, short, unsigned short, bool, and unscoped enums without a fixed underlying type are automatically promoted to a larger integer type. If int can represent all values of the original type, the value promotes to int; otherwise, it promotes to unsigned int. The original value is always fully preserved (cppreference Implicit conversions).

  2. Promotion also plays a key role during overload resolution: an integral promotion such as char -> int takes precedence over a standard conversion like char -> short. To inspect how the compiler performs these promotions behind the scenes, you can inspect the intermediate transformations directly on C++ Insights.

  3. We can verify with code that arithmetic on small integer types and bitwise operations on uint8_t promote to int. Using static_assert and std::is_same_v, we confirm at compile time that the types of a + b (char + char), s + s (short + short), t + t (bool + bool), and ~u (~std::uint8_t) are all int. On this test setup, std::uint8_t is an alias for unsigned char (implementation-defined), and the signedness of plain char is 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
  1. Comparing signed and unsigned integers presents another classic trap caused by promotion rules. When comparing int i = -1; against unsigned char uc = 1; (i < uc), unsigned char has a lower rank than int and int can represent its entire range, so uc promotes to int(1). The comparison becomes -1 < 1, which evaluates to true. However, when comparing int i = -1; with unsigned int ui = 1; (i < ui), unsigned int is not promoted; instead, under the usual arithmetic conversions, the signed i converts to unsigned int. Unsigned conversion is modular, so -1 becomes the maximum value of unsigned int, and the comparison evaluates to false. 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-compare warnings 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 4 steps that turn ~true into true: true (bool) → promotion to int 1 → bitwise NOT gives -2 (two&#x27;s complement) → bool conversion, nonzero so true; to negate use !x

  1. The evaluation of ~true to true proceeds in four distinct steps:

    1. Integral promotion: The bool value true meets the bitwise operator ~ and promotes to the int value 1 (false promotes to 0, true promotes to 1).
    2. Bitwise inversion: A bitwise NOT operation (~1) is performed on int 1. Since C++20 mandates two’s complement as the sole representation for signed integers (cppreference Fundamental types), inverting all bits yields exactly -2.
    3. Boolean conversion: The resulting int -2 is converted back to bool. Because 0 converts to false and any nonzero value converts to true, -2 converts to true.
    4. Conclusion: Hence, static_cast<bool>(~true) == true holds true (Hacker News discussion mirror).
  2. Applying a bitwise NOT to false (~false) yields true through the identical mechanism. false promotes to int 0, ~0 evaluates to -1 in two’s complement, and -1 is nonzero, producing true when converted back to bool. Thus, applying ~ to either true or false always yields true, introducing a critical logical bug.

  3. 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-operation warning on ~ expressions with bool operands, accompanied by the note did 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).

  1. 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 — typically int — during bitwise operations such as Red | Green. When casting that computed int back to the enum via static_cast, if the value falls outside the enumeration’s valid range, the C++ standard defines it as undefined behavior (UB) (cppreference static_cast).

  2. Using a scoped enum class with a fixed underlying type eliminates this danger. Declaring enum 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 leveraging std::underlying_type_t to overload operator| and a has membership test, you can implement type-safe bit flags. In C++23, std::to_underlying from <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)
  1. 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 a bool.
    • Bitmask definitions: When implementing bit flags, use enum class with an explicit fixed underlying type (such as std::uint8_t) instead of an unscoped enum, and overload the required bitwise operators.
    • Signed vs. unsigned comparisons: Remember that small unsigned types like unsigned char promote to int, whereas unsigned int does not; always keep implicit conversion rules in mind when comparing mixed signedness.
    • Enforce compiler warnings: Keep -Wall -Wextra enabled to catch -Wbool-operation and -Wsign-compare warnings during compilation.
  2. 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.