bitburner-src/utils/helpers/isPowerOfTwo.ts

18 lines
400 B
TypeScript
Raw Normal View History

/**
* Determines if the number is a power of 2
* @param n The number to check.
*/
export function isPowerOfTwo(n: number) {
if (isNaN(n)) {
return false;
}
2018-07-08 07:11:34 +02:00
if (n === 0) {
return false;
}
// Disabiling the bitwise rule because it's honestly the most effecient way to check for this.
// tslint:disable-next-line:no-bitwise
2018-07-08 07:11:34 +02:00
return (n & (n - 1)) === 0;
}