I was able to remove the local variable from my solution. So it's possible with no locals or casting and without + (or -, which is not allowed anyway).
But it's pretty unintelligible and has taken...
Type: Posts; User: oogabooga
I was able to remove the local variable from my solution. So it's possible with no locals or casting and without + (or -, which is not allowed anyway).
But it's pretty unintelligible and has taken...
Here's a hint for anyone interested in trying this.
To find the answer, you only have to compare 1 bit -- the highest order bit where the two numbers differ.
So the first trick is to create a...
Excellent. Thanks. Here's a rewrite. The overall alg still seems to work.
// Logical Right-Shift
int lrs(int x, int n) {
if (x < 0) {
x = (x >> 1) & INT_MAX;
n--;
}...
If you've bothered to read the thread you'll know that there are a lot of other cases for which your attempt fails.
I've solved it using just ! ~ & ^ | >>
It assumes 2's complement but it works...
isitGreater(INT_MAX-1, INT_MIN+1) is another example that doesn't work. There are many similar examples.
whiteflag's suggestion (casting to a larger type, which I assume you can't do) gave me an...
Take x is -4, y is 5
Where's the overflow?
It seems to give the correct answer 0.
Your second example (-2147483648) is INT_MIN, which I pointed out won't work since you can't take the 2's...
Obviously it'll fail if x is INT_MIN, but otherwise it seems okay.
Give some specific values for which it fails.