I want alwaysPositive to be assigned a positive number with all possible values for lareValue1 and largeValue2 (these are at least 1).
The following statement causes a buffer overflow:
int alwaysPositive = (largeValue1 + largeValue2) / 2;
I know I can prevent it by substracting and adding:
int alwaysPositive = largeValue1 + ((largeValue2 - largeValue1) / 2);
But in other programming languages I can use an unsigned bitshift to do the trick:
int alwaysPositive3 = (largeValue1 + largeValue2) >>> 1;
How can I do this in C#?
The answers below all solve the problem. There are probably lots of ways to do this, but they all (including my solutions) have one thing in common: they all look obfuscated.