Mathematics is the study of quantity, structure, space, change, and related topics of pattern and form. Mathematicians seek out patterns whether found in numbers, space, natural science, computers, imaginary abstractions, or elsewhere.... , a power of two is any of the integer
Integer
The integers are natural numbers including 0 and their negative and non-negative numberss . They are numbers that can be written without a fractional or decimal component, and fall within the set .... powers
Exponentiation
Exponentiation is a mathematics operation , written 'an', involving two numbers, the base a and the exponentn.... of the number two
2 (number)
2 is a number, numeral, and glyph. It is the natural number following 1 and preceding 3 .... ; in other words, two multiplied
Multiplication
Multiplication is the Operation of scaling one number by another. It is one of the four basic operations in elementary arithmetic .Multiplication is defined for Natural number in terms of repeated addition; for example, 4 multiplied by 3 can be calculated by adding 3 copies of 4 together:... by itself a certain number of times. Note that one is a power (the zeroth power) of two. Written in binary
Binary numeral system
The binary numeral system, or notation with a radix of 2. Owing to its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by all modern computers.... , a power of two always has the form 100...0, just like a power of ten in the decimal
Decimal
The decimal numeral system has 10 as its Base . It is the most widely used numeral system.... system.
Because two is the base of the binary system, powers of two are important to computer science
Computer science
Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems.... . Specifically, two to the power of n is the number of ways the bit
Bit
A bit is a binary numeral system numerical digit, taking a value of either 0 or 1. Binary digits are a basic unit of information Computer data storage and transmission in digital computing and digital information theory.... s in a binary integer of length n can be arranged, and thus numbers that are one less than a power of two denote the upper bounds of integers
Integer (computer science)
In computer science, the term integer is used to refer to a data type which represents some finite subset of the mathematical integers. These are also known as integral data types.... in binary computers (one less because 0, not 1, is used as the lower bound).
Mathematics is the study of quantity, structure, space, change, and related topics of pattern and form. Mathematicians seek out patterns whether found in numbers, space, natural science, computers, imaginary abstractions, or elsewhere.... , a power of two is any of the integer
Integer
The integers are natural numbers including 0 and their negative and non-negative numberss . They are numbers that can be written without a fractional or decimal component, and fall within the set .... powers
Exponentiation
Exponentiation is a mathematics operation , written 'an', involving two numbers, the base a and the exponentn.... of the number two
2 (number)
2 is a number, numeral, and glyph. It is the natural number following 1 and preceding 3 .... ; in other words, two multiplied
Multiplication
Multiplication is the Operation of scaling one number by another. It is one of the four basic operations in elementary arithmetic .Multiplication is defined for Natural number in terms of repeated addition; for example, 4 multiplied by 3 can be calculated by adding 3 copies of 4 together:... by itself a certain number of times. Note that one is a power (the zeroth power) of two. Written in binary
Binary numeral system
The binary numeral system, or notation with a radix of 2. Owing to its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by all modern computers.... , a power of two always has the form 100...0, just like a power of ten in the decimal
Decimal
The decimal numeral system has 10 as its Base . It is the most widely used numeral system.... system.
Because two is the base of the binary system, powers of two are important to computer science
Computer science
Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems.... . Specifically, two to the power of n is the number of ways the bit
Bit
A bit is a binary numeral system numerical digit, taking a value of either 0 or 1. Binary digits are a basic unit of information Computer data storage and transmission in digital computing and digital information theory.... s in a binary integer of length n can be arranged, and thus numbers that are one less than a power of two denote the upper bounds of integers
Integer (computer science)
In computer science, the term integer is used to refer to a data type which represents some finite subset of the mathematical integers. These are also known as integral data types.... in binary computers (one less because 0, not 1, is used as the lower bound). 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 a byte
Byte
A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space.... , which is 8 bits long, being used to store the number, giving a maximum value of 28-1 = 255.
Powers of two also measure computer memory. A byte is eight bits, resulting in the possibility of 256 values (28). A kilobyte is 1,024 (210) bytes (the term "byte" is often defined as a collection of bits
Bit
A bit is a binary numeral system numerical digit, taking a value of either 0 or 1. Binary digits are a basic unit of information Computer data storage and transmission in digital computing and digital information theory.... rather than the strict definition of an 8-bit quantity.) Standards prefer the word kibibyte
Kibibyte
A kibibyte is a unit of information or computer storage, established by the International Electrotechnical Commission in 2000. Its symbol is KiB.... , as "kilobyte" also means 1,000 bytes. Nearly all processor register
Processor register
In computer architecture, a processor register is a small amount of Computer storage available on the CPU whose contents can be accessed more quickly than storage available elsewhere.... s have sizes that are powers of two, 32 or 64 being most common (see word size
Word (computer science)
In computing, "word" is a term for the natural unit of data used by a particular computer design. A word is simply a fixed-sized group of bits that are handled together by the machine.... ).
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.
In mathematics, a prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. An infinitude of prime numbers exists, as demonstrated by Euclid around 300 BC.... that is one less than a power of two is called a Mersenne prime
Mersenne prime
In mathematics, a Mersenne number is a positive integer that is one less than a power of two:Some definitions of Mersenne numbers require that the exponent n be 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
257 (number)
257 is the natural number between 256 and 258. It is also a prime number.... ) that is one more than a power of two is called a Fermat prime. The exponent will itself be a power of two. See Fermat number
Fermat number
In mathematics, a Fermat number, named after Pierre de Fermat who first studied them, is a natural number of the formwhere n is a nonnegative integer.... . A fraction
Fraction (mathematics)
A fraction is a number that can represent part of a whole.The earliest fractions were reciprocals of integers, symbols representing one half, one third, one quarter, and so on.... that has a power of two as its denominator is called a dyadic rational
Dyadic rational
In mathematics, a dyadic fraction or dyadic rational is a rational number whose denominator is a power of two, i.e., a number of the form a/2b where a is an integer and b is a natural number; for example, 1/2 or 3/8, but not 1/3.... .
8 is the natural number, following 7 and preceding 9 . The SI prefix for 10008 is yotta , and for its reciprocal yocto . It is the root of two other numbers: eighteen and eighty ....
Sixteen is a composite number, and a square number, being 4 2 = 4 × 4. It is the smallest number with exactly five divisors, its proper divisors being , , and ....
1024 is the natural number following 1023 and preceding 1025 .1024 is a power of two: .It is also the square of 32: .1024 is the smallest number with exactly 11 divisors ...
220
=
1,048,576
230
=
1,073,741,824
240
=
1,099,511,627,776
250
=
1,125,899,906,842,624
260
=
1,152,921,504,606,846,976
Powers of two whose exponents are powers of two
Because modern memory cells and registers are accessed by a Computer bus
Computer bus
In computer architecture, a bus is a subsystem that transfers data between computer components inside a computer or between computers. Each bus defines its set of connectors to physically plug devices, cards or cables together.... whose width (number of bits) is also a power of two, the most frequent powers of two to appear are those whose exponent is also a power of two
Double exponential function
A double exponential function is a constant raised to the power of an exponential function. The general formula is , which grows even faster than an exponential function.... . For example:
Sixteen is a composite number, and a square number, being 4 2 = 4 × 4. It is the smallest number with exactly five divisors, its proper divisors being , , and ....
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.
A bit is a binary numeral system numerical digit, taking a value of either 0 or 1. Binary digits are a basic unit of information Computer data storage and transmission in digital computing and digital information theory.... s in a byte
Byte
A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space.... , also known as an octet
Octet (computing)
In computing, an octet is a grouping of eight bits.Octet, with the only exception noted below, always refers to an entity having exactly eight bits.... . (The term byte
Byte
A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space.... is often defined as a collection of bits
Bit
A bit is a binary numeral system numerical digit, taking a value of either 0 or 1. Binary digits are a basic unit of information Computer data storage and transmission in digital computing and digital information theory.... rather than the strict definition of an 8-bit quantity, as demonstrated by the term kilobyte
Kilobyte
Kilobyte is a unit of Computer data storage equal to either 1,024 bytes or 1,000 bytes , depending on context.It is abbreviated in a number of ways: KB, kB, K and Kbyte.... .)
210 = 1,024
The binary approximation of the kilo-, or 1,000 multiplier, which causes a change of prefix. For example: 1,024 byte
Byte
A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space.... s = 1 kilobyte
Kilobyte
Kilobyte is a unit of Computer data storage equal to either 1,024 bytes or 1,000 bytes , depending on context.It is abbreviated in a number of ways: KB, kB, K and Kbyte.... (or kibibyte
Kibibyte
A kibibyte is a unit of information or computer storage, established by the International Electrotechnical Commission in 2000. Its symbol is KiB.... ).
This number has no special significance to computers, but is important to humans because we make use of powers of ten.
216 = 65,536
The number of distinct values representable in a single word on a 16-bit
16-bit
16-bit architectureThe HP 2100#Descendants and variants , introduced in 1975, was the world's first 16-bit microprocessor.Prominent 16-bit processors include the PDP-11, Intel 8086, Intel 80286 and the WDC 65C816.... processor, such as the original x86 processors.
In computer programming, a short integer is a data type that can represent a whole number which may take less storage, while having a smaller range, compared with a standard integer on the same machine.... variable in the C
C (programming language)
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.... , C++
C++
C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features.... , C#, and Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java .... programming languages.
220 = 1,048,576
The binary approximation of the mega-, or 1,000,000 multiplier, which causes a change of prefix. For example: 1,048,576 byte
Byte
A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space.... s = 1 megabyte
Megabyte
Megabyte is a SI prefix-multiple of the unit byte for digital information computer storage or transmission and is equal to 106 bytes.... (or mibibyte).
This number has no special significance to computers, but is important to humans because we make use of powers of ten.
Color or colour is the visual perception property corresponding in humans to the categories called red, yellow, blue and others.... s that can be displayed in truecolor
Truecolor
Truecolor is a method of representing and storing graphical image information in an RGB color space such that a very large number of colors, shades, and hues can be displayed in an image, such as in high quality photographic images or complex graphics.... , which is used by common computer monitors.
This number is the result of using the three-channel RGB system, with 8 bits for each channel, or 24 bits in total.
230 = 1,073,741,824
The binary approximation of the giga-, or 1,000,000,000 multiplier, which causes a change of prefix. For example: 1,073,741,824 byte
Byte
A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space.... s = 1 gigabyte
Gigabyte
Gigabyte is an SI prefix-multiple of the unit byte for Computer data storage. Since the giga- prefix means 109, gigabyte means 1,000,000,000 bytes .... (or gibibyte
Gibibyte
Gibibyte is a unit of Computer data storage, abbreviated GiB.The gibibyte is closely related to the gigabyte, which can either be a synonym for gibibyte, or refer to 109 bytes = 1,000,000,000 bytes, depending on context .... ).
This number has no special significance to computers, but is important to humans because we make use of powers of ten.
232 = 4,294,967,296
The number of distinct values representable in a single word on a 32-bit
32-bit
The range of integer values that can be stored in 32 bits is 0 through 4,294,967,295 or -2,147,483,648 through 2,147,483,647 using two's complement encoding.... processor. Or, the number of values representable in a dword (double word) on a 16-bit
16-bit
16-bit architectureThe HP 2100#Descendants and variants , introduced in 1975, was the world's first 16-bit microprocessor.Prominent 16-bit processors include the PDP-11, Intel 8086, Intel 80286 and the WDC 65C816.... processor, such as the original x86 processors.
In computer science, the term integer is used to refer to a data type which represents some finite subset of the mathematical integers. These are also known as integral data types.... variable in the Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java .... programming language.
In computer science, a long integer is a data type that can represent a whole number which may have a larger range, while taking more storage, compared with a standard integer on the same machine.... variable in the C
C (programming language)
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.... , C++
C++
C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features.... , and C# programming languages.
An Internet Protocol address is a numerical identification that is assigned to devices participating in a computer network utilizing the Internet Protocol for communication between its nodes.... es under IPv4
IPv4
Internet Protocol version 4 is the fourth revision in the development of the Internet Protocol and it is the first version of the protocol to be widely deployed.... . Although this is a seemingly large number, IPv4 address exhaustion is imminent.
240 = 1,099,511,627,776
The binary approximation of the tera-, or 1,000,000,000,000 multiplier, which causes a change of prefix. For example: 1,099,511,627,776 byte
Byte
A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space.... s = 1 terabyte
Terabyte
A terabyte is a measurement term for computer storage. The value of a terabyte based upon a decimal radix is defined as one 1000000000000 bytes, or 1000 gigabytes.... (or tebibyte
Tebibyte
A tebibyte is a unit of information or computer storage, abbreviated TiB.The tebibyte is closely related to the terabyte, which can be either a synonym for tebibyte, or refer to 1012 bytes = 1,000,000,000,000 bytes, depending on context .... ).
This number has no special significance to computers, but is important to humans because we make use of powers of ten.
264 = 18,446,744,073,709,551,616
The number of distinct values representable in a single word on a 64-bit
64-bit
64-bit CPUs have existed in supercomputers since the 1960s and in RISC-based computer workstation and Server s since the early 1990s. In 2003 they were introduced to the mainstream personal computer arena, in the form of the x86-64 and 64-bit PowerPC processor architectures.... processor. Or, the number of values representable in a dword (double word) on a 32-bit
32-bit
The range of integer values that can be stored in 32 bits is 0 through 4,294,967,295 or -2,147,483,648 through 2,147,483,647 using two's complement encoding.... processor. Or, the number of values representable in a qword (quadruple word) on a 16-bit
16-bit
16-bit architectureThe HP 2100#Descendants and variants , introduced in 1975, was the world's first 16-bit microprocessor.Prominent 16-bit processors include the PDP-11, Intel 8086, Intel 80286 and the WDC 65C816.... processor, such as the original x86 processors.
In computer science, a long integer is a data type that can represent a whole number which may have a larger range, while taking more storage, compared with a standard integer on the same machine.... variable in the Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java .... programming language.
The total number of IP addresses available under IPv6
IPv6
Internet Protocol version 6 is the next-generation Internet layer protocol for packet -switched internetworking and the Internet. IPv4 is the dominant Internet Protocol version, and was the first to receive widespread use.... giving a bank of possible numbers large enough to handle the number of present network addresses needed without fear of exhaustion.
Fast algorithm to check if a positive number is a power of two
The binary numeral system, or notation with a radix of 2. Owing to its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by all modern computers.... of integers makes it possible to apply a very fast test to determine whether a given positive integer x is a power of two:
The binary numeral system, or notation with a radix of 2. Owing to its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by all modern computers.... of integers makes it possible to calculate the modulos of a non-negative integer (x) with a power of two (y) very quickly:
In computer programming, a bitwise operation operates on one or two bit patterns or Binary numeral system at the level of their individual bits.... . 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 convert any number into nearest power of two number
The following formula finds the nearest power of two with respect to binary logarithm
Binary logarithm
In mathematics, the binary logarithm is the logarithm for base 2. It is the inverse function of .... of a given value :
Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading.... :
POT:= 2^ round(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 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:
In computing, the most significant bit is the bit position in a Binary numeral system having the greatest value. The msb is sometimes referred to as the left-most bit on big-endian architectures, due to the convention in positional notation of writing more significant digits further to the left.... that is "1" from the binary representation of , when means the least significant bit
Least significant bit
In computing, the least significant bit is the bit position in a Binary numeral system integer giving the units value, that is, determining whether the number is even or odd....
Assume that all bits 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
T nearestpower2(T v)
Algorithm to find the next-highest power of two
The pseudocode for an algorithm to compute the next-highest power of two for a particular integer "n" is as follows:
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
2689
0101010000000
2688
0111111000000
4032
0111111110000
4080
0111111111111
4095
1000000000000
4096
As demonstrated above, the algorithm yieds the correct value of 4096. It should be noted that the nearest power to 2689 happens to be 2048; 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
T nexthigher(T k)
For unsigned integers, the code would be:
template
T nexthigher(T k)