Home > Backend Development > C++ > What do & and | mean in C language?

What do & and | mean in C language?

下次还敢
Release: 2024-05-07 10:40:33
Original
775 people have browsed it

The & (bitwise AND) and | (bitwise OR) operators in C language operate on integer binary bits bit by bit: the result of the & operation is 1 if and only if both bits are 1; | The result of the operation is 1 if and only if at least one bit is 1.

What do & and | mean in C language?

& and | operators in C language

& (bitwise AND)## The

#& operator ANDs the binary bits of two given integers bit by bit, and the result is 1 if and only if both corresponding bits are 1.

Syntax:

result = x & y;
Copy after login

Example:

int x = 6; // 0b110
int y = 5; // 0b101
int result = x & y; // 0b100 (4)
Copy after login

| (Bitwise OR)

|Operator ORs the binary bits of two given integers bit by bit, and the result is 1 if and only if at least one corresponding bit is 1.

Grammar:

result = x | y;
Copy after login

Example:

int x = 6; // 0b110
int y = 5; // 0b101
int result = x | y; // 0b111 (7)
Copy after login

Notes:

    & and | operators only apply to integer types. The
  • operator has a higher priority than arithmetic operators.
  • Bitwise operators are usually used for bit operations and masks, for example:

      Check whether a certain bit is 1:
    • if ((x & ( 1 << n)) != 0)
    • Clear a certain bit:
    • x &= ~(1 << n)
    • Set a certain bit:
    • x |= (1 << n)

The above is the detailed content of What do & and | mean in C language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template