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!)

^{n}= 2

^{0}= 1. Then, we calculate the following powers of two by increasing the value of n by one each time: 2

^{n+1}= 2

^{1}= 2, 2

^{n+2}= 2

^{2}= 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

^{n}, 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

^{n}-1. Therefore, we have 2

^{3}-1 = 7 (111 in base-2), 2

^{4}-1 = 15 (1111 in base-2), 2

^{5}-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

## 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 `2`

and to find the exponent ^{n}`n`

, it uses the binary logarithm function `log2(start)`

from the `decimal.js`

library. As `start`

is `2`

, the function ^{n}`log2(2`

finds the starting power ^{n})`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 `2`

. To turn a BigNumber value into a regular string, the ^{i}`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.