getNextPowerOfTwo static method

int getNextPowerOfTwo(
  1. num value
)

Calculates the next power of two for the given value. If value is already a power of two, it is returned. If value is not a power of two, the smallest power of two greater than value is returned.

Implementation

static int getNextPowerOfTwo(num value) {
  if (value is int &&
      value > 0 &&
      (value.toInt() & (value.toInt() - 1)) == 0) {
    return value;
  } else {
    var result = 1;
    value -= 0.000000001; // avoid floating point rounding errors
    while (result < value) {
      result <<= 1;
    }
    return result;
  }
}