log2

Base-2 logarithm.

Category: Mathematics > Elementary Math

Syntax

Y = log2(X)

Returns the base-2 logarithm of each element of X.

Description

log2(x) tells how many times 2 must be doubled to reach x: log2(8) is 3 and log2(10) is about 3.32. It measures information in bits and the number of binary digits needed, as in ceil(log2(n+1)) bits to store the integer n.

For a negative real x the result is complex. nextpow2(x) is ceil(log2(abs(x))), handy for choosing FFT lengths.

Input arguments

X
Numeric array, real or complex.

Output arguments

Y
Array of the same size as X. Exact powers of 2 give exact integers.

Example

Powers of two and bits

log2([1 8 10 1024])
ceil(log2(1000 + 1))
Output
ans = 1×4

         0    3.0000    3.3219   10.0000
ans = 10

See also

log log10 pow2 nextpow2 log1p