powerOfTwo(...) replaced with arithmetic check

val >= 0 && val&(val-1) == 0

Charming :)