silu
- By using bit of & to get the bits that should be incremented
- Find the two logic errors on the second time solve it, without see other’s solutions
- Special cases for negative integers! don’t forget it! See Negative Integer Bit Summary
Negative Integer
- Luckily my algs works for one negative one positive, but does not work for two negatives.
- Why it’s ok for one negative one positive?
- I tried to find it in the format of negative integer by bit representation. However, then I find my while condition is
increase > 0
which actually isincrease != 0
in my mind. I ignore the negative inputs and then ignore the conditions in my code. - Next time: if find ignore some input cases, check if missing any condition cases also.
Very good negative integer bits manipulation summary
https://discuss.leetcode.com/topic/49771/java-simple-easy-understand-solution-with-explanation
Logic Loopholes
- In my first time solve it, I want to make
sum = a&b; sum &= ~increase
to get rid of the 1 in intersection . It turned out that ~increase is 32 bits long, which is not wise. - When both numbers have 1 in the same position, after added, it should turned to 0 instead of 1. So, we can use a^b instead of a&b
- When updating the sum and increase, they should all use the old values. Pay attention not use their updated values.
Failed Cases
- -12 -8 Don’t forget negatives