“将带符号整数操作数与二进制按位运算符一起使用” -使用无符号短型 [英] "Use of a signed integer operand with a binary bitwise operator" - when using unsigned short
问题描述
在下面的C代码片段中,该代码片段检查是否设置了16位序列的前两位:
In the following C snippet that checks if the first two bits of a 16-bit sequence are set:
bool is_pointer(unsigned short int sequence) {
return (sequence >> 14) == 3;
}
CLion的Clang-Tidy给我使用带符号整数操作数二进制按位运算符警告,我不明白为什么。 unsigned short
是否不够签名?
CLion's Clang-Tidy is giving me a "Use of a signed integer operand with a binary bitwise operator" warning, and I can't understand why. Is unsigned short
not unsigned enough?
推荐答案
此检查的代码似乎注意是否对按位运算符的 操作数进行了签名。引起警告的原因不是顺序
,而是 14
,您可以通过使 14
通过在末尾附加 u
来取消签名。
The code for this check seems to care if either operand to the bitwise operator is signed. It is not sequence
causing the warning, but 14
, and you can alleviate the problem by making 14
unsigned by appending a u
to the end.
(sequence >> 14u)
此警告是错误的。正如 Roland的答案所述,CLion正在解决此问题。
This warning is bad. As Roland's answer describes, CLion is fixing this.
这篇关于“将带符号整数操作数与二进制按位运算符一起使用” -使用无符号短型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!