rspec/rules/S2437/csharp/rule.adoc

36 lines
1.8 KiB
Plaintext
Raw Normal View History

2023-06-01 17:53:53 +02:00
== Why is this an issue?
Certain https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators[bitwise operations] are not needed and should not be performed because their results are predictable.
2023-06-07 15:41:34 +02:00
Specifically, using `& -1` with any value always results in the original value.
2023-06-01 17:53:53 +02:00
That is because the binary representation of `-1` on a https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types[integral numeric type] supporting negative numbers, such as `int` or `long`, is based on https://en.wikipedia.org/wiki/Two%27s_complement[two's complement] and made of all 1s: `0b111...111`.
Performing `&` between a value and `0b111...111` means applying the `&` operator to each bit of the value and the bit `1`, resulting in a value equal to the provided one, bit by bit.
[source,csharp]
----
anyValue & -1 // Noncompliant
anyValue // Compliant
----
Similarly, `anyValue | 0` always results in `anyValue`, because the binary representation of `0` is always `0b000...000` and the `|` operator returns its first input when the second is `0`.
[source,csharp]
----
anyValue | 0 // Noncompliant
anyValue // Compliant
----
The same applies to `anyValue ^ 0`: the `^` operator returns `1` when its two input bits are different (`1` and `0` or `0` and `1`) and returns `0` when its two input bits are the same (both `0` or both `1`).
When `^` is applied with `0`, the result would be `1` if the other input is `1`, because the two input bits are different, and `0` if the other input bit is `0`, because the two input are the same. That results in returning `anyValue`.
[source,csharp]
----
anyValue ^ 0 // Noncompliant
anyValue // Compliant
----
include::../resources-dotnet.adoc[]
include::../rspecator.adoc[]