There was a time-limited online assessment. When I submitted this solution, I passed 6 test cases of 12 test cases.
it is C++ clode, C/=2 would be a non-integer result because C is not of doubel type.
Yea, using bitwise operators is better.
the range of A and B is [0,1000000000]
I dont understand the after-the-shift part.
Also, yes, the test would come before the shift, unless you were testing the carry flag, but I don't think C can do that.
@Acer
For example, after the shift left, if you want to test the MSB (most-significant bit) of an 8-bit result that looks like this:
10011000
You could do a logical AND with a mask that looks like this:
10000000 , which is 2^(8-1)
The result is zero if the MSB is zero.
There may be a specific C++ operation that can directly check the MSB without using the mask, but I don't remember if there is or not.
Note: The 2^(n-1) allows for an arbitrary number of bits, but you'd probably know that already in advance and could just use a constant.