# C++'s complement of true is true, except when GCC says it's false

Original title: The complement of true is true, except when it's false

> Hacker News story 49953344 · language: en · submitted 2026-10-04
> Web version: https://zeli.app/story/49953344
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 72 · by [aw1621107](<https://news.ycombinator.com/user?id=aw1621107>) · 💬 28
[original](<https://dryperspective.github.io/posts/complement-of-true/>) · [HN discussion](<https://news.ycombinator.com/item?id=49953344>)

## Summary

A standards proposal for bitmask operations on enums leads to a deep dive into C++ integer promotion quirks. Integral promotion forces small types up to int, and boolean conversion maps any nonzero value to true. So ~true becomes true. But GCC disagrees for enums backed by bool, truncating instead of converting, and unscoped enums can trigger undefined behavior when bitwise results fall outside their value range.

## Key quote

> There you have it, the complement of true is true, or spelled in C++ static_cast<bool>(~true) == true.

## HN discussion (28 comments)

### [Panzerschrek](<https://news.ycombinator.com/item?id=49978427>)

> With such example:
>
> bool Complement( bool x )
>
> {
>
> return ~x;
>
> }
>
> GCC gives a warning if compiling with -Wall:
>
> test.cpp:3:10: warning: ‘~’ on an expression of type bool [-Wbool-operation]
>
> return ~x;
>
> So, it's surprise that C++ does this, but ones who enable compiler warnings are not in trouble.

### [rep_lodsb](<https://news.ycombinator.com/item?id=49975949>)

> They should have defined true as -1 (all bits set) instead, as in some other languages like Forth and BASIC.

### [s-zeng](<https://news.ycombinator.com/item?id=49976943>)

> In any field of characteristic 2, every element is its own negative. So on the surface this makes sense, notwithstanding the unintuitive enum corner case behaviour

---

## Cite

- Page: https://zeli.app/story/49953344
- Date: 2026-10-04
- Source: dryperspective.github.io (https://dryperspective.github.io/posts/complement-of-true/)
- Hacker News: https://news.ycombinator.com/item?id=49953344

## Other languages

- zh: https://zeli.app/zh/story/49953344 · markdown: https://zeli.app/zh/story/49953344.md
- ja: https://zeli.app/ja/story/49953344 · markdown: https://zeli.app/ja/story/49953344.md
- ko: https://zeli.app/ko/story/49953344 · markdown: https://zeli.app/ko/story/49953344.md
- de: https://zeli.app/de/story/49953344 · markdown: https://zeli.app/de/story/49953344.md
- es: https://zeli.app/es/story/49953344 · markdown: https://zeli.app/es/story/49953344.md
- ru: https://zeli.app/ru/story/49953344 · markdown: https://zeli.app/ru/story/49953344.md
