Take a look at the downvoted-to-0 formulation I gave upthread, then see if you can program it that way tomorrow without looking, and then think it through to see if it could possibly be wrong, and once you're satisfied it's correct, try testing it. Probably you'll never need to implement binary search yourself again, but it's a good exercise for thinking through algorithms. You can probably get it working that way in under an hour instead of a whole day.