![]() | ||
In mathematics, a power of two means a number of the form 2n where n is an integer, i.e. the result of exponentiation with number two as the base and integer n as the exponent.
Contents
- Expressions and notations
- Computer science
- Mersenne primes
- Euclids Elements Book IX
- The first 96 powers of two
- Powers of 1024
- Powers of two whose exponents are powers of two
- Some selected powers of two
- Fast algorithm to check if a positive number is a power of two
- Fast algorithm to find a number modulo a power of two
- Algorithm to find a power of two nearest to a number
- Algorithm to find a power of two greater than or equal to a number
- Fast algorithms to round any integer to a multiple of a given power of two
- Other properties
- References
In a context where only integers are considered, n is restricted to non-negative values, so we have 1, 2, and 2 multiplied by itself a certain number of times.
Because two is the base of the binary numeral system, powers of two are common in computer science. Written in binary, a power of two always has the form 100…000 or 0.00…001, just like a power of ten in the decimal system.
Expressions and notations
Verbal expressions, mathematical notations, and computer programming expressions using a power operator or function include:
2 to the n2 to the power of n2 power npower(2, n)pow(2, n)2n1 << n2 ^ n2 ** n2 [3] n2 ↑ nA(n - 3, 3) + 3Computer science
Two to the power of n, written as 2n, is the number of ways the bits in a binary word of length n can be arranged. A word, interpreted as an unsigned integer, can represent values from 0 (000…0002) to 2n − 1 (111…1112) inclusively. Corresponding signed integer values can be positive, negative and zero; see signed number representations. Either way, one less than a power of two is often the upper bound of an integer in binary computers. As a consequence, numbers of this form show up frequently in computer software. As an example, a video game running on an 8-bit system might limit the score or the number of items the player can hold to 255—the result of using a byte, which is 8 bits long, to store the number, giving a maximum value of 28 − 1 = 255. For example, in the original Legend of Zelda the main character was limited to carrying 255 rupees (the currency of the game) at any given time, and the video game Pac-Man famously shuts down at level 255.
Powers of two are often used to measure computer memory. A byte is now considered eight bits (an octet, resulting in the possibility of 256 values (28). (The term byte once meant (and in some cases, still means) a collection of bits, typically of 5 to 32 bits, rather than only an 8-bit unit.) The prefix kilo, in conjunction with byte, may be, and has traditionally been, used, to mean 1,024 (210). However, in general, the term kilo has been used in the International System of Units to mean 1,000 (103). Binary prefixes have been standardized, such as kibi (Ki) meaning 1,024. Nearly all processor registers have sizes that are powers of two, 32 or 64 being most common.
Powers of two occur in a range of other places as well. For many disk drives, at least one of the sector size, number of sectors per track, and number of tracks per surface is a power of two. The logical block size is almost always a power of two.
Numbers that are not powers of two occur in a number of situations, such as video resolutions, but they are often the sum or product of only two or three powers of two, or powers of two minus one. For example, 640 = 512 + 128 = 128 × 5, and 480 = 32 × 15. Put another way, they have fairly regular bit patterns.
Mersenne primes
A prime number that is one less than a power of two is called a Mersenne prime. For example, the prime number 31 is a Mersenne prime because it is 1 less than 32 (25). Similarly, a prime number (like 257) that is one more than a positive power of two is called a Fermat prime—the exponent itself is a power of two. A fraction that has a power of two as its denominator is called a dyadic rational. The numbers that can be represented as sums of consecutive positive integers are called polite numbers; they are exactly the numbers that are not powers of two.
Euclid's Elements, Book IX
The geometric progression 1, 2, 4, 8, 16, 32, … (or, in the binary numeral system, 1, 10, 100, 1000, 10000, 100000, … ) is important in number theory. Book IX, Proposition 36 of Elements proves that if the sum of the first n terms of this progression is a prime number (means, a Mersenne prime mentioned above), then this sum times the nth term is a perfect number. For example, the sum of the first 5 terms of the series 1 + 2 + 4 + 8 + 16 = 31, which is a prime number. The sum 31 multiplied by 16 (the 5th term in the series) equals 496, which is a perfect number.
Book IX, Proposition 35, proves that in a geometric series if the first term is subtracted from the second and last term in the sequence, then as the excess of the second is to the first—so is the excess of the last to all those before it. (This is a restatement of our formula for geometric series from above.) Applying this to the geometric progression 31, 62, 124, 248, 496 (which results from 1, 2, 4, 8, 16 by multiplying all terms by 31), we see that 62 minus 31 is to 31 as 496 minus 31 is to the sum of 31, 62, 124, 248. Therefore, the numbers 1, 2, 4, 8, 16, 31, 62, 124 and 248 add up to 496 and further these are all the numbers that divide 496. For suppose that p divides 496 and it is not amongst these numbers. Assume p q is equal to 16 × 31, or 31 is to q as p is to 16. Now p cannot divide 16 or it would be amongst the numbers 1, 2, 4, 8 or 16. Therefore, 31 cannot divide q. And since 31 does not divide q and q measures 496, the fundamental theorem of arithmetic implies that q must divide 16 and be amongst the numbers 1, 2, 4, 8 or 16. Let q be 4, then p must be 124, which is impossible since by hypothesis p is not amongst the numbers 1, 2, 4, 8, 16, 31, 62, 124 or 248.
The first 96 powers of two
(sequence A000079 in the OEIS)
One can see that starting with 2 the last digit is periodic with period 4, with the cycle 2–4–8–6–, and starting with 4 the last two digits are periodic with period 20. These patterns are generally true of any power, with respect to any base. The pattern continues, of course, where each pattern has starting point 2k, and the period is the multiplicative order of 2 modulo 5k, which is φ(5k) = 4 × 5k−1 (see Multiplicative group of integers modulo n).
Powers of 1024
(sequence A140300 in the OEIS)
The first few powers of 210 are a little more than those of 1000:
See also IEEE 1541-2002.
Powers of two whose exponents are powers of two
Because data (specifically integers) and the addresses of data are stored using the same hardware, and the data is stored in one or more octets (23), double exponentials of two are common. For example,
(sequence A001146 in the OEIS)
21 = 222 = 424 = 1628 = 256216 = 65,536232 = 4,294,967,296264 = 18,446,744,073,709,551,616 (20 digits)2128 = 340,282,366,920,938,463,463,374,607,431,768,211,456 (39 digits)2256 =115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,457,584,007,913,129,
639,936 (78 digits)2512 =
13,407,807,929,942,597,099,574,024,998,205,846,127,479,365,820,592,393,377,723,561,443,721,764,
030,073,546,976,801,874,298,166,903,427,690,031,858,186,486,050,853,753,882,811,946,569,946,433,
649,006,084,096 (155 digits)21,024 = 179,769,313,486,231,590,772,931,...,304,835,356,329,624,224,137,216 (309 digits)22,048 = 32,317,006,071,311,007,300,714,...,193,555,853,611,059,596,230,656 (617 digits)24,096 = 1,044,388,881,413,152,506,691,...,243,804,708,340,403,154,190,336 (1,234 digits)28,192 = 1,090,748,135,619,415,929,462,...,997,186,505,665,475,715,792,896 (2,467 digits)216,384 = 1,189,731,495,357,231,765,085,...,460,447,027,290,669,964,066,816 (4,933 digits)232,768 = 1,415,461,031,044,954,789,001,...,541,122,668,104,633,712,377,856 (9,865 digits)265,536 = 2,003,529,930,406,846,464,979,...,339,445,587,895,905,719,156,736 (19,729 digits)2131,072 = 4,014,132,182,036,063,039,166,...,850,665,812,318,570,934,173,696 (39,457 digits)2262,144 = 16,113,257,174,857,604,736,195,...,559,730,753,862,605,349,934,298 (78,914 digits)2524,288 = 259,637,056,783,100,077,612,659,...,596,209,011,369,814,364,528,226 (157,827 digits)21,048,576 = 67,411,401,254,990,734,022,690,...,009,289,119,068,940,335,579,136 (315,653 digits)
Several of these numbers represent the number of values representable using common computer data types. For example, a 32-bit word consisting of 4 bytes can represent 232 distinct values, which can either be regarded as mere bit-patterns, or are more commonly interpreted as the unsigned numbers from 0 to 232 − 1, or as the range of signed numbers between −231 and 231 − 1. Also see tetration and lower hyperoperations. For more about representing signed numbers see two's complement.
In a connection with nimbers these numbers are often called Fermat 2-powers.
The numbers
converges to an irrational number. Despite the rapid growth of this sequence, it is the slowest-growing irrationality sequence known.
Some selected powers of two
int
variable in the Java and C# programming languages.The range of a Cardinal
or Integer
variable in the Pascal programming language.The minimum range of a long integer variable in the C and C++ programming languages.The total number of IP addresses under IPv4. Although this is a seemingly large number, IPv4 address exhaustion is imminent.The number of binary operations with domain equal to any 4-element set, such as GF(4).17,498,005,798,264,095,394,980,017,816,940,970,922,825,355,447,145,699,491,406,164,851,279,623,
993,595,007,385,788,105,416,184,430,592
Fast algorithm to check if a positive number is a power of two
The binary representation of integers makes it possible to apply a very fast test to determine whether a given positive integer x is a power of two:
positive x is a power of two ⇔ (x & (x − 1)) is equal to zero.where & is a bitwise logical AND operator. Note that if x is 0, this incorrectly indicates that 0 is a power of two, so this check only works if x > 0.
Examples:
Proof of Concept:
Proof uses the technique of contrapositive.
Statement, S: If x&(x-1) = 0 and x is an integer greater than zero then x = 2k (where k is an integer such that k>=0).
Concept of Contrapositive:
S1: P -> Q is same as S2: ~Q -> ~P
In above statement S1 and S2 both are contrapositive of each other.
So statement S can be re-stated as below
S': If x is a positive integer and x ≠ 2k (k is some non negative integer)then x&(x-1) ≠ 0
Proof:
If x ≠ 2k then at least two bits of x are set.(Let's assume m bits are set.)
Now, bit pattern of x - 1 can be obtained by inverting all the bits of x up to first set bit of x (starting from LSB and moving towards MSB, this set bit inclusive).
Now, we observe that expression x & (x-1) has all the bits zero up to the first set bit of x and since x & (x-1) has remaining bits same as x and x has at least two set bits hence predicate x & (x-1) ≠ 0 is true.
Fast algorithm to find a number modulo a power of two
As a generalization of the above, the binary representation of integers makes it possible to calculate the modulos of a non-negative integer (x) with a power of two (y) very quickly:
x mod y = (x & (y − 1)).where & is a bitwise logical AND operator. This bypasses the need to perform an expensive division. This is useful if the modulo operation is a significant part of the performance critical path as this can be much faster than the regular modulo operator.
Algorithm to find a power of two nearest to a number
The following formula finds the nearest power of two, on a logarithmic scale, of a given value x > 0:
This should be distinguished from the nearest power of two on a linear scale. For example, 23 is nearer to 16 than it is to 32, but the previous formula rounds it to 32, corresponding to the fact that 23/16 = 1.4375, larger than 32/23 = 1.3913.
If x is an integer value, following steps can be taken to find the nearest value (with respect to actual value rather than the binary logarithm) in a computer program:
- Find the most significant bit position k, that is set (1) from the binary representation of x, when {{{1}}} means the least significant bit
- Then, if bit k − 1 is zero, the result is 2k. Otherwise the result is 2k + 1.
Algorithm to find a power of two greater than or equal to a number
Sometimes it is desired to find the least power of two that is not less than a particular integer, n. The pseudocode for an algorithm to compute the next-higher power of two is as follows. If the input is a power of two it is returned unchanged.
Where | is a binary or operator, >> is the binary right-shift operator, and bitspace is the size (in bits) of the integer space represented by n. For most computer architectures, this value is either 8, 16, 32, or 64. This operator works by setting all bits on the right-hand side of the most significant flagged bit to 1, and then incrementing the entire value at the end so it "rolls over" to the nearest power of two. An example of each step of this algorithm for the number 2689 is as follows:
As demonstrated above, the algorithm yields the correct value of 4,096. The nearest power to 2,689 happens to be 2,048; however, this algorithm is designed only to give the next highest power of two to a given number, not the nearest.
Another way of obtaining the 'next highest' power of two to a given number independent of the length of the bitspace is as follows.
Fast algorithms to round any integer to a multiple of a given power of two
For any integer, x and integral power of two, y, if z = y - 1,
x to a multiple of y.
Other properties
The sum of all n-choose binomial coefficients is equal to 2n. Consider the set of all n-digit binary integers. Its cardinality is 2n. It is also the sums of the cardinalities of certain subsets: the subset of integers with no 1s (consisting of a single number, written as n 0s), the subset with a single 1, the subset with two 1s, and so on up to the subset with n 1s (consisting of the number written as n 1s). Each of these is in turn equal to the binomial coefficient indexed by n and the number of 1s being considered (e.g., there are 10-choose-3 binary numbers with ten digits that include exactly three 1s).
The number of vertices of an n-dimensional hypercube is 2n. Similarly, the number of (n − 1)-faces of an n-dimensional cross-polytope is also 2n and the formula for the number of x-faces an n-dimensional cross-polytope has is
The sum of the reciprocals of the powers of two is 2. The sum of the reciprocals of the squared powers of two is 1/3.
The smallest natural power of two whose decimal representation begins with 7 is