bitburner-src/utils/helpers/isPowerOfTwo.ts

18 lines
387 B
TypeScript
Raw Normal View History

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