I often have to generate a sequence of powers of two so I created this simple utility that does it for me. It lets you generate however many powers of two you need, starting from any value. It works in the browser and is powered by alien technology from the future.
Powers Of Two Generator Options
Powers Of Two Generator Examples (click to try!)
1; 2; 4; 8; 16; 32; 64; 128; 256; 512; 1024; 2048; 4096; 8192; 16384; 32768; 65536; 131072; 262144; 524288
0.5 0.25 0.125 0.0625 0.03125 0.015625 0.0078125 0.00390625 0.001953125 0.0009765625
111 1111 11111 111111 1111111 11111111 111111111 1111111111 11111111111 111111111111 1111111111111 11111111111111
How Does This Powers Of Two Generator Work?
starting value can be any power of two
2n and to find the exponent
n, it uses the binary logarithm function
log2(start) from the
decimal.js library. As
2n, the function
log2(2n) finds the starting power
n and puts it in the
startPower variable. If the starting power is not an integer, it uses the
toFixed(0, rm) method, which turns the decimal into an integer using the rounding mode
Decimal.ROUND_DOWN. Next, it creates an empty array
powersOfTwo =  and starts a loop with the starting value
i = startPower and the condition
powersOfTwo.length < count. If the increasing sequence mode is selected, the counter variable increases
i++; if the decreasing sequence mode is selected, the counter decreases
i--. Another helper library
bignumber.js makes sure that the powers of two work with large numbers. Before the loop starts, another variable is setup
two = new BigNumber(2) and then inside the loop,
two.pow(i) is called to calculate
2i. To turn a BigNumber value into a regular string, the
toString(base) function is used, where
base is the selected radix from 2 to 64. All the calculated values are
push()ed at the end of the
powersOfTwo array and before being printed to the screen, they are
join(separator)ed together (
separator is specified in options).
Created by Browserling
This powers of two generator was created by me and my team at Browserling. Behind the scenes, it's actually powered by our web developer tools that are used by millions of people every month. Browserling itself is an online cross-browser testing service powered by alien technology. Check it out!
Secret message: If you love my tools, then I love you, too! Use coupon code TOOLLING to get a discount at my company.