# \[Java\] Avoid bitwise operators

**URL:** <https://community.sonarsource.com/t/java-avoid-bitwise-operators/18642>\
**Category:** New rules / language support\
**Tags:** rules, java\
**Created:** [January 11, 2020, 1:57pm UTC](https://community.sonarsource.com/t/java-avoid-bitwise-operators/18642 "2020-01-11T13:57:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bannmann](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/bannmann/32/249_2.png) [@bannmann](https://community.sonarsource.com/u/bannmann)\
**Post date:** [January 11, 2020, 1:57pm UTC](https://community.sonarsource.com/t/java-avoid-bitwise-operators/18642/1 "2020-01-11T13:57:33Z")

</div>

Hi,

I would like to propose a new rule for SonarJava. While it is certainly _not_ a candidate for inclusion in the default Sonar Way quality profile, I believe it will still be useful to many teams and organizations, including my own company.

If the rule gets approved, I will finish up my proof of concept implementation and create a PR.

Here are the details:

**Rule description & motivation**

Java’s bitwise operators (and/or/xor, left/right shift, unsigned right shift and corresponding compound assignment operators) have several valid uses, e.g. binary file formats, cryptography algorithms or graphics programming.

That said, many developers working outside these areas are not accustomed to these operators because their application code usually has no need for them. In such applications, performing bitwise manipulation of numbers may be an indicator of premature optimization attempts and/or developers who are more familiar with low-level coding than with object-oriented concepts or the Java API (e.g. `enum` and `EnumSet<MyEnum>` as a replacement for the classic bit fields approach).

For teams who are working in such contexts, SonarJava should therefore offer a rule that alerts them whenever bitwise operators are used. This way, the code can be verified to be a justified and correctly implemented use of bit manipulation, or replaced with more obvious and/or type-safe high-level code.

**Noncompliant Code**

```auto
public int double(int base) {
    return original << 1;
}

class Style {
    public static final int BOLD = 1;
    public static final int ITALICS = 2;
    public static final int UNDERLINE = 4;
}

public void demo() {
    int heading = Style.BOLD | Style.UNDERLINE;
    apply(heading);
}

public void apply(int styles) {
    if ((styles & Style.BOLD) == Style.BOLD) applyBold();
    if ((styles & Style.ITALICS) == Style.ITALICS) applyItalics();
    if ((styles & Style.UNDERLINE) == Style.UNDERLINE) applyUnderline();
}

```

**Compliant Code**

```auto
public int double(int base) {
    return original * 2; 
}

enum Style {
    BOLD, ITALICS, UNDERLINE;
}

public void demo() {
    Set<Style> heading = EnumSet.of(Style.BOLD, Style.UNDERLINE);
    apply(heading);
}

public void apply(Set<Style> styles) {
    if (styles.contains(Style.BOLD)) applyBold();
    if (styles.contains(Style.ITALICS)) applyItalics();
    if (styles.contains(Style.UNDERLINE)) applyUnderline();
}

```

**Impact to keep this code as it is**  
Code which needlessly uses bit manipulation tends to make the author’s intention less obvious than the object-oriented equivalent. Also, it increases the risk that future changes introduce bugs, e.g. adding another flag to an `int` bit field but accidentally using the same value as an existing one.

**Notes**  
No issue should be raised when the and/or/xor operators or their compound assignment counterparts are used with `boolean` values.

**References**

- [Vojtech Ruzicka’s Programming Blog: Bit Manipulation in Java – Bitwise and Bit Shift operations](https://www.vojtechruzicka.com/bit-manipulation-java-bitwise-bit-shift-operations/)
- [Oracle: Guide to Typesafe Enums](https://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html) - lists problems of the bit flag approach, explains `enum` in detail

**Type**  
Code Smell

**Tags**  
clumsy, pitfall

---

<div class="post-metadata">

**Author:** ![Nicolas\_Harraudeau](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/nicolas_harraudeau/32/255_2.png) [@Nicolas\_Harraudeau](https://community.sonarsource.com/u/Nicolas_Harraudeau)\
**Post date:** [January 13, 2020, 9:12am UTC](https://community.sonarsource.com/t/java-avoid-bitwise-operators/18642/2 "2020-01-13T09:12:53Z")

</div>

Hi @bannmann,

Thank you for suggesting this rule. As a java developer I agree with the ideas that bitwise operators should be used carefully and premature optimization is the root of all evil.

However, as you said this rule cannot be part of Sonar Way because it would raise too many false positives. There are many cases where bitwise operations are perfectly valid, even in projects where the use of bitwise operators is rare. Developers would have to either disable the rule or mark issues as False Positives/Won’t Fix. We don’t have, and don’t intend to add, a “Reviewed” status for Code Smells and Bugs. This would make SonarQube an audit tool.

I’m afraid that we can’t accept the rule as it is.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![bannmann](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/bannmann/32/249_2.png) [@bannmann](https://community.sonarsource.com/u/bannmann)\
**Post date:** [January 19, 2020, 11:58am UTC](https://community.sonarsource.com/t/java-avoid-bitwise-operators/18642/3 "2020-01-19T11:58:14Z")

</div>

Hi @Nicolas_Harraudeau,

I must say I’m surprised of your answer, and respectfully ask you to reconsider your decision.

Before I give my rationale, I’d like to emphasize that none of the following, no matter how critical it is, was intended to insult or disrespect you or anyone else personally, or to talk down any of the decisions, processes or products at SonarSource. Instead, my goal is to shed some light on a few aspects that you maybe just haven’t thought of yet. Also, I’d like to understand the criteria you have for what makes rules acceptable so I can direct my future efforts towards the right things.

Let’s dive in.

1. Your answer can be read as if you’re stating that a new rule would only be considered if it is suitable for Sonar Way. However, that probably wasn’t your intention as it’s obvious that SonarQube _does_ offer rules that are opt-in because of their controversial nature: of all Java code smell rules currently available on SonarCloud, 211 are active in Sonar Way and 130 are not. Limited to the ones added in 2018 and 2019, the numbers are 44 to 14.

2. The distinction between “rule raises many false positives” or “is useful” highly depends on the purpose of the codebase and the coding standards of the team maintaining it.  
  
To demonstrate this, I’d like to look at the results of applying a selection of rules **not part of Sonar Way** , and the “avoid bitwise operators” rule I proposed, to two codebases: first, the **`sonarqube-6.5`** codebase used for SonarJava integration tests, second, my company’s primary codebase for which I’ll use the alias **`corporate`** here.  
  
For each rule, I’ll also give my personal opinion on whether the rule could/should be activated and/or how much work checking the issues would be.  
  
To make the issue numbers comparable despite the different codebase sizes (200K vs 62K SLOC), I’m going to indicate the issue “density” which I calculate as `issue_count / SLOC * 1000`.

3. 

> There are many cases where bitwise operations are perfectly valid, even in projects where the use of bitwise operators is rare. Developers would have to either disable the rule or mark issues as False Positives/Won’t Fix.

  - Then again, there are also many cases where e.g. the ternary operator or octal number literals are perfectly valid, but still SonarQube offers optional code smell rules that flag each and every use of these language features ([S1774](https://rules.sonarsource.com/java/RSPEC-1774) and [S1314](https://rules.sonarsource.com/java/RSPEC-1314) respectively). I think that the ternary operator is even more commonly used in the Java world than bitwise operators, so this feels like a contradiction. How does SonarSource decide whether an optional “do not use this” code smell rule makes sense for a given language feature?
  - Also, if we’re in a project “where the use of bitwise operators is _rare_”, the rule would obviously _rarely_ raise issues. I don’t understand why it’s a problem to mark these rare issues as “Won’t Fix”. Isn’t that precisely the point of “Won’t Fix”? For example, with the existing SonarQube rules below, developers are likely to do the exact same thing:
    - A team activates [S1774](https://rules.sonarsource.com/java/RSPEC-1774) (“The ternary operator should not be used”) because they feel the ternary operator makes their code harder to read. However, they may accept it in some cases where avoiding it would make the code unnecessarily verbose (e.g. the occasional algorithm, or a `hashCode()` implementation), so they mark it as “Won’t Fix” in these cases.
    - When [S4926](https://rules.sonarsource.com/java/RSPEC-2057) (“serialVersionUID” should not be declared blindly) is active, any reported issue would usually be fixed. However, it sometimes may instead be marked as “Won’t fix”, e.g. if a particular class needs to be backwards compatible (deserializing an instance from the previous release) and therefore keep its hard-coded serialVersionUID.

4. 

> Developers would have to (…) disable the rule.

  - My interpretation of the [“plug the leak” metaphor](https://blog.sonarsource.com/water-leak-changes-the-game-for-technical-debt-management/) is that just because a certain rule raises a large number of issues, _you would never disable that rule_. The reason is that when a team uses the approach recommended by SonarSource in the linked article (TLDR: fix issues in new code, ignore issues in old unchanging code), that rule still helps them to not introduce more problems with new code, and helps them fix the issues along the way when they touch old code (the “boy scout rule”).

5. 

> We don’t have, and don’t intend to add, a “Reviewed” status for Code Smells and Bugs. This would make SonarQube an audit tool.

  - I don’t think such a _ **Reviewed** _ status would be necessary for this rule or any other rule. The existing statuses **Open** (“set by SonarQube on new issues”) and **Confirmed** (“set manually to indicate that the issue is valid”) are perfectly sufficient (see documentation on [Issues lifecycle](https://docs.sonarqube.org/latest/user-guide/issues/)).  
But maybe I’m missing your point here. Could you please explain why adding this rule would only make sense if SonarQube had a _ **Reviewed** _ status? I’d especially like to understand why on the other hand [S1774](https://rules.sonarsource.com/java/RSPEC-1774) (“The ternary operator should not be used”) does **not** need such a status.
  - In theory, a _ **Reviewed** _ status would be required if SonarQube can’t really judge a given piece of code, and not even say “this looks suspicious” - this is the case with Security Hotspots. However, all the examples above are rules which alert to deviations from the usual approach and coding conventions of a team, which is always suspicious. So if the coding conventions says “don’t use bitwise operators” and the codebase has 0 bitwise operator usages, a rule enforcing this would not equal to auditing - it would just help keeping the desired status quo.

I apologize that this post got so lengthy, but hope it was worth the read.

Please let me know what you think!

Kind regards,  
Jens

---

<div class="post-metadata">

**Author:** ![Nicolas\_Harraudeau](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/nicolas_harraudeau/32/255_2.png) [@Nicolas\_Harraudeau](https://community.sonarsource.com/u/Nicolas_Harraudeau)\
**Post date:** [January 31, 2020, 10:19am UTC](https://community.sonarsource.com/t/java-avoid-bitwise-operators/18642/4 "2020-01-31T10:19:39Z")

</div>

Hi @bannmann,

Don’t worry, challenging our decisions is perfectly ok and it enables us to improve and clarify our approach. In this case my first answer should have explained better why I don’t think that this rule can be included as it is.

Regarding point 1., forget what I said about Sonar Way. Let’s talk more about the value of the rule from different angles.

**False Positives:** Let’s suppose that we are in a codebase with no bitwise operators. After a review developers accepted that some algorithm should be optimized using bitwise operators. A common use case is to use bitwise operators with some flags (example in [android code](https://android.googlesource.com/platform/frameworks/base/+/2b6d358d559f31e203f97af0865e67e24c59377b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java#2585)). The first time an issue is raised it correctly asks developers to review the code. However when new flags are added and the algorithm has to be modified, every additional issue is a False Positive because the algorithm was already accepted. The only solution is to disable the rule for the whole file and hope that it won’t spread in other files.

**User experience:** Rules which ask for a review from developers should be extremely rare. In our experience developers stop to look at issues when too many of them are not actual issues to fix. It is sometimes acceptable to have a rule raising a warning when the impact of the issue is extremely high (ex: [S4926 “serialVersionUID” should not be declared blindly](https://rules.sonarsource.com/java/RSPEC-4926) where an ID can make serialization fail). However it is less justifiable to add this burden on developers when the impact is not clear, such as a sign of potential early optimization.  
Even for Security Hotspot rules, which are designed to ask developers to review their code when it is security sensitive, we strive to be as precise as possible in order to reduce the number of issues. When a Security Hotspot rule raises too many issues on safe code we simply remove the rule.

**But we could add the rule. People are free to enable it when they want.** Every rule in SonarQube is maintained and migrated when we refactor our code. For example it took months to migrate every rule when we made SonarJava rely on the Eclipse frontend (available in SonarJava 6.0) and the migration of rules is still in progress for SonarJS after we moved to ESLint frontend last year. There is also the cost of constantly evaluating and testing rules at SonarSource as we improve them. Thus we have sometime to refuse rules when their value seems lower than the cost of having an additional rule. This is obviously subjective and we try to be as transparent as possible.

Even rules which have already been implemented can be removed when their value is too low. For example we started discussing the value of [S2039 Member variable visibility should be specified](https://rules.sonarsource.com/java/RSPEC-2039) some time ago and it will be probably deprecated and later removed.

I would recommend to use the custom rules API, which was specifically designed for users who want a rule which we can’t accept in the default rule sets.

Does this make sense to you?

---

<div class="post-metadata">

**Author:** ![bannmann](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/bannmann/32/249_2.png) [@bannmann](https://community.sonarsource.com/u/bannmann)\
**Post date:** [January 31, 2020, 5:07pm UTC](https://community.sonarsource.com/t/java-avoid-bitwise-operators/18642/5 "2020-01-31T17:07:08Z")

</div>

Hi Nicolas!

> [@Nicolas\_Harraudeau](#):
>
> Does this make sense to you?

Yes, indeed. I could argue with some details of the points you raised, but your overall conclusion would still be reasonable and justified. Thanks for explaining the rationale in more detail!

> [@Nicolas\_Harraudeau](#):
>
> I would recommend to use the custom rules API, which was specifically designed for users who want a rule which we can’t accept in the default rule sets.

Unfortunately, unless I’m mistaken, one does not have that option on SonarCloud.

Being a rather small company, we have a very strong preference to rely on cloud services instead of maintaining our own servers wherever possible. I would not want us to move to a self-hosted SonarQube instance unless there is a truly huge advantage. And as much as I would like to activate an “avoid bitwise operators” rule in my company, that is not at all important enough to warrant a move and the subsequent maintenance burden.

That said, since adopting SonarCloud, we have encountered quite a few cases where closed-source code had specific mistakes and undesired code patterns, but was too specific to the respective company/team to become a SonarCloud rule. So we would definitively appreciate being able to “inject/upload/connect/webhook/sandbox” a custom rule to SonarCloud in some way. Of course, I have no idea if that would even be possible or whether there are (enough) other SonarCloud subscribers interested in this. Also, I have to admit that I would consider most of [my](https://community.sonarsource.com/t/ldap-or-google-g-suite-login/3888) [various](https://community.sonarsource.com/t/improve-rule-configuration-for-non-connected-projects/5464) [other](https://community.sonarsource.com/t/how-to-use-organization-for-both-bitbucket-github/12714) [feature](https://community.sonarsource.com/t/notification-settings-per-organization/821) [suggestions](https://community.sonarsource.com/t/allow-making-quality-profiles-gates-public-on-sonarcloud/1874) to be more important. (Sorry, I couldn’t resist advertising them here 😉 ).
