++

Where is the problem with this function?
I only got 50% of the test cases passed.

@Acer

>"I only got 50% of the test cases passed."

Does this mean that 50% produced an error, or that 50% produced the wrong result?

Just looking at it without actually testing it myself, I suspect the problem is in the divide by 2, (C/=2;) which would be a non-integer result about 50% of the time.

Perhaps a better way is to use bitwise operators. Use bitwise shift left (<<) and then test if it is greater than 2^(n-1). (where n=number of bits). Or optionally, after the shift do a logical AND (&) with a mask that has the highest bit set, then compare, etc.

@Pat

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.

Follow

@Acer

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.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.