SonarQube Java Analyzer, Rule S2183, why should I remove this useless shift?

I wrote:

public static final int MY_GREAT_COLOR = (91 << 16) + (155 << 8) + 213 + (255 << 32); 

And the sonar says:

New: Squid: S2183 Severity: CRITICAL, Message: Remove this useless shift

Can someone tell me why? Is it just the fact that there shouldn't be any calculations in the literature, even if it adds readability?

+6
source share
2 answers

I assume 255 is your alpha channel, in this case, I think you would like 255 << 24 .

SonarQube does not know your intentions, but it has a valid point, as Toby stated.

As for readability, you can also try hexa-code (for example, 0xFF5B9BD5 ), many developers are already used to it. Of course, a decimal would be completely unreadable and ugly.

+2
source

255 << 32 overflows Java (32-bit) int , becoming null.

Do you want long and 255L << 32 ?

+2
source

Source: https://habr.com/ru/post/989813/


All Articles