In mathematics, a power of two is any of the integer powers of the number two;[1] in other words, two multiplied by itself a certain number of times.[2] By definition, the number one is a power (the zeroth power) of two.
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…0, just like a power of ten in the decimal system.
Two to the power of n, written as 2n, is the number of ways the bits in a binary integer of length n can be arranged; thus, as positive numbers and zero are stored, one less than a power of two is the upper bound of an integers 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.
Powers of two are often used to measure computer memory. A byte is now considered to be eight bits (an octet, resulting in the possibility of 256 values (28). (The term byte has been, and in some case continues to be, used to be 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 System International to mean 1,000 (103. Binary prefixes have been standardised, such as kibi' meaning 1,024. Nearly all processor registers have sizes that are powers of two, 32 or 64 being most common (see word size).
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 which 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, and 480 = 32 × 15. Put another way, they have fairly regular bit patterns.
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 power of two is called a Fermat prime; the exponent will itself be 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.
| 20 | = | 1 | 212 | = | 4,096 | 224 | = | 16,777,216 | 236 | = | 68,719,476,736 | 248 | = | 281,474,976,710,656 | 260 | = | 1,152,921,504,606,846,976 | |||||
| 21 | = | 2 | 213 | = | 8,192 | 225 | = | 33,554,432 | 237 | = | 137,438,953,472 | 249 | = | 562,949,953,421,312 | 261 | = | 2,305,843,009,213,693,952 | |||||
| 22 | = | 4 | 214 | = | 16,384 | 226 | = | 67,108,864 | 238 | = | 274,877,906,944 | 250 | = | 1,125,899,906,842,624 | 262 | = | 4,611,686,018,427,387,904 | |||||
| 23 | = | 8 | 215 | = | 32,768 | 227 | = | 134,217,728 | 239 | = | 549,755,813,888 | 251 | = | 2,251,799,813,685,248 | 263 | = | 9,223,372,036,854,775,808 | |||||
| 24 | = | 16 | 216 | = | 65,536 | 228 | = | 268,435,456 | 240 | = | 1,099,511,627,776 | 252 | = | 4,503,599,627,370,496 | 264 | = | 18,446,744,073,709,551,616 | |||||
| 25 | = | 32 | 217 | = | 131,072 | 229 | = | 536,870,912 | 241 | = | 2,199,023,255,552 | 253 | = | 9,007,199,254,740,992 | 265 | = | 36,893,488,147,419,103,232 | |||||
| 26 | = | 64 | 218 | = | 262,144 | 230 | = | 1,073,741,824 | 242 | = | 4,398,046,511,104 | 254 | = | 18,014,398,509,481,984 | 266 | = | 73,786,976,294,838,206,464 | |||||
| 27 | = | 128 | 219 | = | 524,288 | 231 | = | 2,147,483,648 | 243 | = | 8,796,093,022,208 | 255 | = | 36,028,797,018,963,968 | 267 | = | 147,573,952,589,676,412,928 | |||||
| 28 | = | 256 | 220 | = | 1,048,576 | 232 | = | 4,294,967,296 | 244 | = | 17,592,186,044,416 | 256 | = | 72,057,594,037,927,936 | 268 | = | 295,147,905,179,352,825,856 | |||||
| 29 | = | 512 | 221 | = | 2,097,152 | 233 | = | 8,589,934,592 | 245 | = | 35,184,372,088,832 | 257 | = | 144,115,188,075,855,872 | 269 | = | 590,295,810,358,705,651,712 | |||||
| 210 | = | 1,024 | 222 | = | 4,194,304 | 234 | = | 17,179,869,184 | 246 | = | 70,368,744,177,664 | 258 | = | 288,230,376,151,711,744 | 270 | = | 1,180,591,620,717,411,303,424 | |||||
| 211 | = | 2,048 | 223 | = | 8,388,608 | 235 | = | 34,359,738,368 | 247 | = | 140,737,488,355,328 | 259 | = | 576,460,752,303,423,488 | 271 | = | 2,361,183,241,434,822,606,848 |
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,
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.
int variable in the Java and C# programming languages.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:
(x & (x − 1)) equals 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:
|
|
|
1…111…1 |
|
|
1…111…111…1 | |
|
|
|
0…010…0 |
|
|
0…010…010…0 | |
|
|
|
0…001…1 |
|
|
0…010…001…1 | |
|
|
|
0…000…0 |
|
|
0…010…000…0 |
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 & (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.
The following formula finds the nearest power of two with respect to binary logarithm of a given value x > 0:
![2^{\mathrm{round}[\log_2 (x)]}](http://upload.wikimedia.org/math/d/0/0/d00751ec9e09659ccc7f4eed017fe7b1.png)
Computer pseudocode:
pot = 2^roundup(log2(npot));
It does not, however, find the nearest power of two with respect to the actual value. For example, 47 is nearer to 32 than it is to 64, but the previous formula rounds it to 64.
If
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:
, that is set (1) from the binary representation of
, when
means the least significant bit
are zero. Then, if bit
is zero, the result is
. Otherwise the result is
.A C++ version of this code for the unsigned integer type T would be:
template <class T> T nearestpower2(T v) { int k; if (v == 0) return 1; for (k = sizeof(T) * 8 - 1; ((static_cast<T>(1U) << k) & v) == 0; k--); if (((static_cast<T>(1U) << (k - 1)) & v) == 0) return static_cast<T>(1U) << k; return static_cast<T>(1U) << (k + 1); }
The pseudocode for an algorithm to compute the next-highest power of two for a particular integer, n, is as follows:[4]
n = n - 1; n = n | (n >> 1); n = n | (n >> 2); n = n | (n >> 4); n = n | (n >> 8); n = n | (n >> 16); ... n = n | (n >> (bitspace / 2)); n = n + 1;
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:
| Binary representation | Decimal representation |
|---|---|
| 0101010000001 | 2,689 |
| 0101010000000 | 2,688 |
| 0111111000000 | 4,032 |
| 0111111110000 | 4,080 |
| 0111111111111 | 4,095 |
| 1000000000000 | 4,096 |
As demonstrated above, the algorithm yields the correct value of 4,096. It should be noted that 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 power of two.
A C++ version of this code for the signed integer type T would be:
template <class T> T nexthigher(T k) { k--; for (int i=1; i<sizeof(T)*CHAR_BIT; i<<=1) k = k | k >> i; return k+1; }
For unsigned integers, the code would be:
template <class T> T nexthigher(T k) { if (k == 0) return 1; k--; for (int i=1; i<sizeof(T)*CHAR_BIT; i<<=1) k = k | k >> i; return k+1; }
Note: CHAR_BIT is defined in <limits.h>
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
.
stock | retire | vm
Why are we here?
All text is available under the terms of the GNU Free Documentation License
This page is cache of Wikipedia. History