Binary operator "<<"

  • build-wrapper-win-x86-64, version 6.3
  • SonarQube Scanner

I am scanning C code for an embedded device, compiled through gcc, the code is simply the following:

hystpf = 3 << 3;

The following bug is reported: “Identical sub-expressions on both sides of operator “<<””.
This is not a bug and instead improves code readability when fixed-point math implementation is involved, i.e. hystpf is a 16 bit variable and will contain the value of 3 represented in fixed-point math format 13.3

Hello @mannuan,

In many situations, code looking like the one you posted could indeed be a bug, this is why we decided to signal it a such. In your case, it seems to be a legitimate case, so this is a false positive that you can ignore in the UI of SonarQube.

However, if I were reviewing your code, I would probably say that the purpose of this line is not obvious, and I would suggest to wrap this operation into a function whose name would make the intent clearer (something like to13_3, maybe?). If you do so, a nice side effect is that the code will no longer be detected as a violation of this rule, since the left hand side of << would become a parameter, while the right hand side would remain the literal 3.

I hope this helps,


Hello @JolyLoic,

while I still don’t see when/why this code could be a bug in C, the rest of your comment is perfectly clear and helps for sure.