EPIJudge
EPIJudge copied to clipboard
Java Powerset: Cleaner way of finding index of lowest set bit
Hi, Not sure if this is the best way of suggesting a solution improvement but here goes :)
Instead of explicitly calculating a Log base 2 on the lowest set bit, use the builtin Integer.numberOfTrailingZeros
. This is a cleaner and (IMO) more intuitive way of calculating the index of the lowest set bit, since its easy to reason about (eg if there is 1 trailing zero, the lowest set bit is at index 1 etc). It is also faster, and although that isn't quite as relevant here, is definitely best practice if this sort of thing was ever actually needed.
This change would require some rewrite of the paragraph beforehand
Hi @jcdavis ,
Many thanks for this idiomatic way of using Java library. I agree this is more intuitive way of the indexing as we are looking for the first bit is set to 1. However, there is one small thing I may disagree with you is the way shall not be faster; because under the hood, the implementation of this function shall not be faster than the bitwise AND operation we had before.
This change will be released in the next version.
Hi @jcdavis,
I'd like to accept your PR, but please, resolve merge conflicts against dev branch
@tsunghsienlee in fact the bit fiddling approach @jcdavis has suggested is much faster than the log approach (a factor of 20 in mysterious benchmarking). This is because the log approach requires computing log (expensive power series) as well as an expensive cast from float to int.