What do & and | mean in C language?
May 02, 2024 pm 05:21 PMThe & (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.
& 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;
Example:
int x = 6; // 0b110 int y = 5; // 0b101 int result = x & y; // 0b100 (4)
| (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;
Example:
int x = 6; // 0b110 int y = 5; // 0b101 int result = x | y; // 0b111 (7)
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)
- x &= ~(1 << n)
- x |= (1 << n)
- if ((x & ( 1 << n)) != 0)
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Usage of typedef struct in c language

The difference between strcpy and strcat in c language

How to implement the power function in C language

What to do if there is an error in scanf in C language
