주요 콘텐츠

pow2

R2026b

Base 2 power and scaling for fixed-point data

Description

Y = pow2(X) returns 2 raised to the power of each element of X such that Y = 2X, where X is a real-valued fi object.

B = pow2(A,k) scales A raised to the power of k, where k is an integer and A and B are fi objects. The operation is performed as a bit shift.

Note

In fixed-point arithmetic, shifting by k bits is equivalent to and more computationally efficient than computing B = A*2k.

Note

For base 2 power and scaling of floating-point numbers, use the pow2 function.

example

Examples

collapse all

Since R2026b

Compute 2x for a fixed-point number.

x = fi(1.5,1,16,10);
y = pow2(x)
y = 

          2.82843017578125
      numerictype(1,51,18)

Create a fixed-point vector and evaluate the element-wise base 2 power.

x = fi([0 0.5 1 1.5 2],1,16,12)';
y = pow2(x)
y = 

                         1
          1.41421508789062
                         2
          2.82843017578125
                         4
      numerictype(1,27,18)

Scale real-valued fixed-point significand by 2 raised to the power of exponent.

In this example, a is a real-valued fi object, and k is a positive integer.

a = fi(pi,1,16,8);
b = pow2(a,3)
b = 

   25.1250

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 8

The pow2 function shifts the bits of a three places to the left, effectively multiplying a by 2 3 .

binary_a = bin(a)
binary_b = bin(b)
binary_a =

    '0000001100100100'


binary_b =

    '0001100100100000'

In this example, a is a real-valued fi object, and k is a negative integer.

a = fi(pi,1,16,8);
b = pow2(a,-4)
b = 

    0.1953

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 8

The pow2 function shifts the bits of a four places to the right, effectively multiplying a by 2 - 4 .

binary_a = bin(a)
binary_b = bin(b)
binary_a =

    '0000001100100100'


binary_b =

    '0000000000110010'

Use the pow2 function with a complex fi object.

format long g
P = fipref('NumericTypeDisplay','short');
a = fi(57-2i,1,16,8);
pow2(a,2)
ans = 

               127.99609375 -                     8i
      numerictype(1,16,8)

Input Arguments

collapse all

Since R2026b

Fixed-point exponent values for base 2 power, specified as a scalar, vector, matrix, or multidimensional array. X must be a real-valued fi object with binary-point scaling.

Data Types: fi

Significand values, specified as a scalar, vector, matrix, or multidimensional array.

The scaling of A must be equivalent to binary point-only scaling, so it must have a power of 2 slope and a bias of 0. fi objects of data type Boolean are not supported.

A can be real or complex for this syntax. If A is complex, the pow2 function operates on both the real and complex portions of A.

Data Types: fi
Complex Number Support: Yes

Exponent value, specified as an integer scalar.

If k is a noninteger value, the pow2 function rounds the value to floor before performing the calculation.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fi

Output Arguments

collapse all

Since R2026b

Base 2 power result, returned as a scalar, vector, matrix, or multidimensional array. The output Y is a signed fi object with the same dimensions as X. The output word length and fraction length are determined automatically based on the input type to preserve precision.

Data Types: fi

Scaled result, returned as a scalar, vector, matrix, or multidimensional array. The output B is a fi object with the same dimensions as A. The output B always has the same word length and fraction length as the input A.

Data Types: fi
Complex Number Support: Yes

Tips

  • For the two-argument syntax, the pow2 function obeys the OverflowAction and RoundingMethod properties associated with the fi object A. If obeying the RoundingMethod property associated with A is not important, use the bitshift function.

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a

expand all

See Also

| | | | | | | |