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

Direction and Rounding
Start, Count, and Separator
Output Base

## Powers Of Two Generator Examples (click to try!)

Increasing Powers of Two
In this example, we generate an increasing list of powers of two. We enter the starting value equal to 1, which corresponds to the zeroth power of two: 2n = 20 = 1. Then, we calculate the following powers of two by increasing the value of n by one each time: 2n+1 = 21 = 2, 2n+2 = 22 = 4, …. When the list reaches twenty elements, we print the calculated powers of two, using a semicolon as a list element separator.
`1; 2; 4; 8; 16; 32; 64; 128; 256; 512; 1024; 2048; 4096; 8192; 16384; 32768; 65536; 131072; 262144; 524288`
Increasing Powers of Two
Round All Bits
Starting power of two.
How many powers of two to generate?
Separate powers of two with this character.
Decimal Base
Decreasing Powers of Two
This example generates a sequence of 2n, where n is decreasing. It starts with the value of 0.5. It's easy to see that the exponent n for the starting value is -1 (because 2-1 = 1/2 = 0.5). Then it decreases the power to n = -2, and we get 2-2 = 1/4 = 0.25, then to n = -3, and we get 2-3 = 1/8 = 0.125, and so on.
```0.5
0.25
0.125
0.0625
0.03125
0.015625
0.0078125
0.00390625
0.001953125
0.0009765625```
Decreasing Powers of Two
Round All Bits
Starting power of two.
How many powers of two to generate?
Separate powers of two with this character.
Decimal Base
Binary Mersenne Numbers
In this example, we use the option "Round All Bits" to generate a list of Mersenne numbers in base two. This mode subtracts one from each power of two via the formula 2n-1. Therefore, we have 23-1 = 7 (111 in base-2), 24-1 = 15 (1111 in base-2), 25-1 = 31 (11111 in base-2). In the output, we get an interesting sequence that consists of repeating ones called binary repunits.
```111
1111
11111
111111
1111111
11111111
111111111
1111111111
11111111111
111111111111
1111111111111
11111111111111```
Increasing Powers of Two
Round All Bits
Starting power of two.
How many powers of two to generate?
Separate powers of two with this character.
Binary Base

## How Does This Powers Of Two Generator Work?

This powers of two generator works entirely in your browser and is written in JavaScript. To generate a list of powers of two, it first parses and checks the options, such as the `start` and `count`. The `start`ing 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 `start` is `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_UP` or `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.