[Solved] Solution differences in byte array to int conversion


Both will give the same result, but I’d choose the | always, because you are dealing with bits, not numbers.

In order to understand why they are equivalent you first have to understand binary numbers and binary maths:

var b = 0b0011; //binary 0011

Ok, lets do something similar to your code:

var s = b + (b << 2);

Step by step:

0011 < 2 is 1100

And to evaluate 1100 | 0011, you simply or every bit with its counterpart.
The result is 1111

Now, what’s (0011 < 2) + 1100? Well, do normal addition but remember, this is binary numbers so we carry over twos not tens:

 0011 +
 1100
 ———-
 1111

You see now why both will always give the same result? There will never be any carry overs so addition and oring is essentially the same: 0 + 1 and 0|1 give the same result, but again, they are totally different operations.

0

solved Solution differences in byte array to int conversion