
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
For what it's worth I did a few tests to get the performance of all proposed solutions.
Performance is added as comment behind the appropriate statement. Bit manipulation works only on integers so the last 3 statements are somewhat out of league, but you'll get the impression. Fastest working solution is MathAbs().
number = MathAbs(number); or number = number*(number>=0) -number*(number<0);
//num = -num; // 60000
//num = -num; // 60000
This one works only if the value is known to be negative, same as with
//int n = ~i + 1; // 83000
So in the sense of OP's question both are not valid solutions.
Same with clearing the highest bit, that wouldn't work for all integer values.
Without the code we can only speculate what's going on at machine level.
int n = ~i + 1; // 83000