The << and >> operators in C: <<: Left shift operator, left shift by the specified number of digits, which is equivalent to multiplying by the power of 2;>> : Right shift operator, shifts the specified number of digits to the right, which is equivalent to dividing by the power of 2, discarding the decimal part.
##<< and >> operators in C
Question: What do the << and >> operators in C mean?
Answer:
<< Operator: left shift operator
Moves a number to Shift left by the specified number of places. Every time you shift one position to the left, the number will be multiplied by 2.
>> Operator: Right shift operator
Moves a number to the right by the specified number of digits. - Every time you shift one position to the right, the number will be divided by 2 and the decimal part will be discarded.
-
Purpose of bit shift operator
- Left shift operator (<<):Used for fast multiplication In powers of 2.
- Right shift operator (>>): For quick division by a power of 2.
- Extract binary bit fields: Through right shift and masking operations, specific bit fields of binary numbers can be easily extracted.
Example:
int x = 10; // 十进制 10
// 左移 3 位(乘以 8)
int y = x << 3; // 结果:80
// 右移 2 位(除以 4)
int z = x >> 2; // 结果:2
Copy after login
Note:
The left shift operator can only be used Integer type. - The right shift operator can be used for integer and unsigned integer types.
- For signed integers, the behavior of the right shift operator depends on the compiler and platform.
-
The above is the detailed content of What do << and >> mean in c++. For more information, please follow other related articles on the PHP Chinese website!