GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScript/Bit-Manipulation/IsPowerOfTwo.js at master · TheAlgorithms/JavaScript Expand file tree/ Copy pathIsPowerOfTwo.jsMore file actions 27 lines (20 loc) · 933 Bytes / Copy pathIsPowerOfTwo.jsFile metadata and controls27 lines (20 loc) · 933 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 /* author: @Aayushi-Mittal This script will check whether the given number is a power of two or not. A number will be a power of two if only one bit is set and rest are unset. This is true for all the cases except 01 because (2^0 = 1) which is not a power of 2. For eg: 10 (2^1 = 2), 100 (2^2 = 4), 10000 (2^4 = 16) Reference Link: https://www.hackerearth.com/practice/notes/round-a-number-to-the-next-power-of-2/ If we will subtract 1 from a number that is a power of 2 we will get it's 1's complement. And we know that 1's complement is just opp. of that number. So, (n & (n-1)) will be 0. For eg: (1000 & (1000-1)) 1 0 0 0 // Original Number (8) 0 1 1 1 // After Subtracting 1 (8-1 = 7) _______ 0 0 0 0 // will become 0 */ export const IsPowerOfTwo = (n) => { return n > 0 && (n & (n - 1)) === 0 } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |