- C language
- Basic grammar
- here
Bitwise operator
This section describes bit operators in C language. Bitwise operations are operations performed in 1-bit units.
| Bit AND operator | & |
|---|---|
| Bit OR operator | | |
| Bit XOR operator | ^ |
| Bit NOT operator | ~ |
Bit AND operator
The bit AND operator "&" sets 1 at the corresponding position of the value to be returned if both of the bits contained in the two operands are 1, and 0 if either of the bits is 0. Set.
#Bit AND x & y
Let's assume that both x and y are 8-bit unsigned integer type. z is the resulting value of the bit AND operator. If both the bits at the x and y positions are 1, the corresponding z bits are also 1, otherwise they are 0.
x 01001100 y 01000101 z 01000100
A sample bit AND operator.
#include <stdio.h>
#include <stdint.h>
int main (void) {
// 01001100
uint8_t x = 0x4C;
// 01000101
uint8_t y = 0x45;
// 01000100
uint8_t z = x & y;
printf("%X\n", z);
}
This is the output result. The output is in hexadecimal, but if it is binary, it is "01000100".
44
Bit OR operator
The bit OR operator "|" is set to 1 at the corresponding position of the value to be returned when either of the bits contained in the two operands is 1 (both may be 1), and both of the bits are set. If is 0, set it to 0.
# Bit OR x | y
Let's assume that both x and y are 8-bit unsigned integer type. z is the resulting value of the bit OR operator. If either the bit at the position of x or y is 1, the corresponding bit of z is also 1, and if both are 0, it is 0.
x 01001100 y 01000101 z 01001101
This is a sample of the bit OR operator.
#include <stdio.h>
#include <stdint.h>
int main (void) {
// 01001100
uint8_t x = 0x4C;
// 01000101
uint8_t y = 0x45;
// 01001101
uint8_t z = x | y;
printf("%X\n", z);
}
This is the output result. The output is in hexadecimal, but if it is binary, it is "01001101".
4D
Bit XOR operator
The bit XOR operator "^" is set to 1 at the corresponding position of the value to be returned if only one of the bits contained in the two operands is 1, and both of the bits are 0 or 1. If so, set it to 0.
#Bit XOR x ^ y
Let's assume that both x and y are 8-bit unsigned integer type. z is the resulting value of the bit XOR operator. If only one bit at one of the x and y positions is 1, the corresponding z bit is also 1, and if both are 0 or 1, it is 0.
x 01001100 y 01000101 z 00001001
A sample bit XOR operator.
#include <stdio.h>
#include <stdint.h>
int main (void) {
// 01001100
uint8_t x = 0x4C;
// 01000101
uint8_t y = 0x45;
// 0001001
uint8_t z = x ^ y;
printf("%X\n", z);
}
This is the output result. The output is in hexadecimal, but if it is binary, it is "0001001".
9
Bit NOT operator
The bit NOT operator "~" inverts a bit contained in the operand.
# Bit NOT ~ x
Let's assume that both x and y are 8-bit unsigned integer type. z is the result value of the bit NOT operator. The x bit is inverted.
x 01001100 z 10110011
This is a sample of the bit NOT operator. The output result is bit ANDed with "0xFF" to eliminate the effect of integer type extension. ..
#include <stdio.h>
#include <stdint.h>
int main (void) {
// 01001100
uint8_t x = 0x4C;
// 01000101
uint8_t y = 0x45;
// 10110011
uint8_t z = ~ x;
printf("%X\n", z);
}
This is the output result. The output is in hexadecimal, but if it is binary, it is "01000100".
B3
Special operator that combines bit operator and "="
There are special operators that combine bitwise operators with "=".
# Same meaning as "x = x & y" x & = y # Same meaning as "x = x | y" x | = y # Same meaning as "x = x ^ y" x ^ = y
When do you use bitwise operators in practice?
The most common use of bitwise operators is when passing flags to function arguments. In C language, flags are often passed as integers, but in that case, the meaning is determined by the position of the bit. Use enum enum to define a flag for each bit. 1 (1 in binary), 2 (10 in binary), 4 (100 in binary), 8 (1000 in binary).
Bit OR "| =" to set each bit, and bit XOR "^ =" to drop each bit.
Use "&" to check if a bit is set.
Sample code to create a flag to pass to a function argument.
#include <stdio.h>
#include <stdint.h>
enum {
MYAPP_FLAG1 = 1,
MYAPP_FLAG2 = 2,
MYAPP_FLAG3 = 4,
MYAPP_FLAG4 = 8,
MYAPP_FLAG5 = 16,
MYAPP_FLAG6 = 32,
MYAPP_FLAG7 = 64,
MYAPP_FLAG8 = 128,
MYAPP_FLAG9 = 256,
MYAPP_FLAG10 = 512,
};;
int main (void) {
uint32_t flag = 0;
// Build MYAPP_FLAG4
flag | = MYAPP_FLAG4;
// 00000000 00000000 00000000 00001000
printf("%X\n", flag);
// Build MYAPP_FLAG10
flag | = MYAPP_FLAG10;
// 00000000 00000000 00000010 00001000
printf("%X\n", flag);
// Drop MyAPP_FLAG4
flag ^ = MYAPP_FLAG4;
// 00000000 00000000 00000010 00000000
printf("%X\n", flag);
// Check if the bit is set
if (flag & MYAPP_FLAG10) {
printf("MYAPP_FLAG10 ok\n");
}
}
C Language Zemi