Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Bitwise OR operator |

The | operator combines the bits of two integers. It can set flags without clearing other bits.

Version: C23; repository compiler flag, production runtime not attested

Syntax

left | right

left | right sets a result bit when at least one corresponding operand bit is set. Both operands must have integer types; usual arithmetic conversions determine their common type and the result type.

In the example, 4u | 3u produces 7u: in binary, 0100 | 0011 produces 0111. The | operator itself leaves its operands unchanged; combined stores the result.

Do not confuse | with logical ||, which returns an int value of 0 or 1 and short-circuits. The | operator evaluates both operands but does not guarantee left-to-right evaluation.

Examples

#include <stdio.h>

int main(void)
{
    unsigned int flags = 4u;
    unsigned int mask = 3u;
    unsigned int combined = flags | mask;

    printf("%u\n", combined);
    return 0;
}

Result

7

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