1

I want to split a long long (a 64-bit value) into two ints (32-bit each). For this I'm using the "standard way" of shifting the long long right by 32 to get the upper 32-bit and cast the long long to an int to get the lower 32-bit. However, clang-tidy reports the shift as warning:

Use of a signed integer operand with a binary bitwise operator

My code is the following:

void split_long_long(long long value, int &upper, int &lower) {
    upper = (int) (value >> 32); // <- Warning here at "value"
    lower = (int) value;
}

How would you rewrite this to avoid the warning?

Note that writing

void split_long_long(unsigned long long int value, unsigned int &upper, unsigned int &lower) {
    upper = (unsigned int) (value >> 32);
    lower = (unsigned int) value;
}

does not fix the warning either, even though there are only unsigned variables in use.

phuclv
  • 37,963
  • 15
  • 156
  • 475
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • Alright, the following code no longer produces a warning: `void split_long_long(unsigned long long int value, int &upper, int &lower) { upper = (int) (value >> 32u); lower = (int) value; }` – BullyWiiPlaza Jul 29 '18 at 16:27
  • Use unions - more readable and safe – 0___________ Jul 29 '18 at 18:50
  • There is no problem with the `unsigned` version you posted. What compiler switches did you use to generate a warning about that code? (Edit the question to show the compilation command) – M.M Jul 29 '18 at 21:53
  • 2
    @M.M: None in particular, `clang-tidy` is part of the code inspections in `CLion` IDE which are enabled by default: https://www.jetbrains.com/clion – BullyWiiPlaza Jul 30 '18 at 08:06

0 Answers0