5 ms·
Can't it simply be written like this? mid = low/2 + high/2
by altaltalt 4y ago
Can't it simply be written like this?
mid = low/2 + high/2
- curling_grad 4y agoFor low=3, high=5 case, this gives mid=3.
- dataflow 4y agoNope, try low = 1, high = 1 and you get mid = 0.
- benmmurphy 4y agoi think you can fix it with: (low >> 1) + (high >> 1) + (low & 1 & high) for unsigned numbers. not sure if it works for signed numbers.
- deleted 4y ago[deleted]
- Godel_unicode 4y agoDivision is not associative: https://www.khanacademy.org/math/arithmetic-home/multiply-divide/properties-of-multiplication/a/associative-property-of-multiplication-review https://www.khanacademy.org/math/arithmetic-home/multiply-di...
- mimon 4y agoWhile that is true it is not relevant here, since this example does not involve associativity. What is relevent here is that integer division is not distributive over addition.