Function that returns the minimum absolute difference between n and any power of 2
static int minDiff ( int n ) { int low = prevPowerof2 ( n ) ; int high = nextPowerOf2 ( n ) ; return Math . Min ( n - low , high - n ) ; }