Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

compl — alternative spelling of ~

compl is a built-in C++ alternative spelling of ~. For integer operands, it flips bits after integral promotions. It is not logical negation and requires neither a header nor a macro definition.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

compl cast-expression
// Equivalent spelling: ~cast-expression

compl expression means the same as ~expression: the spelling differs, not the behavior. compl is a reserved language token, not a function or macro; it cannot be used as a variable name. No header is required.

The built-in operator accepts an operand of integral or unscoped enumeration type. It first performs integral promotions, then flips every bit of the promoted value. The result has the promoted operand's type. Consequently, an unsigned char operand can produce an int result rather than an unsigned char result.

Do not confuse compl with not (!): the former flips bits, whereas the latter performs logical negation. In the example, (compl value) & mask keeps only the lowest four bits of the complemented value: 0101 becomes 1010, or decimal 10. Without the mask, all the remaining bits of the unsigned type would also be flipped.

This entry targets C++23. The supplied source is an evolving working draft, not a frozen C++23 text. The compiler flag identifies the target standard; it does not attest to behavior in a production runtime.

Examples

#include <iostream>

int main() {
    constexpr unsigned value = 0b0101u;
    constexpr unsigned mask = 0b1111u;
    constexpr unsigned result = (compl value) & mask;
    static_assert(result == 0b1010u);
    static_assert((compl value) == (~value));
    std::cout << result << '\n';
}

Result

10

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources