"Use of a signed integer operand with a binary bitwise operator" - when using unsigned short

SakoDaemon picture SakoDaemon · May 17, 2018 · Viewed 22.6k times · Source

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'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?

Answer

Ryan Haining picture Ryan Haining · May 17, 2018

The code for this warning checks 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)

This warning is bad. As Roland's answer describes, CLion is fixing this.