Main Content

pow2

Efficient fixed-point multiplication by 2K

Syntax

b = pow2(a,K)

Description

b = pow2(a,K) returns the value of a shifted by K bits where K is an integer and a and b are fi objects. The output b always has the same word length and fraction length as the input a.

Note

In fixed-point arithmetic, shifting by K bits is equivalent to, and more efficient than, computing b = a*2K.

If K is a non-integer, the pow2 function will round it to floor before performing the calculation.

The scaling of a must be equivalent to binary point-only scaling; in other words, it must have a power of 2 slope and a bias of 0.

a can be real or complex. If a is complex, pow2 operates on both the real and complex portions of a.

The pow2 function obeys the OverflowAction and RoundingMethod properties associated with a. If obeying the RoundingMethod property associated with a is not important, try using the bitshift function.

The pow2 function does not support fi objects of data type Boolean.

The function also does not support the syntax b = pow2(a) when a is a fi object.

Examples

Example 1. Example 1

In the following example, a is a real-valued fi object, and K is a positive integer.

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

a = fi(pi,1,16,8)
b = pow2(a,3)
binary_a = bin(a)
binary_b = bin(b)
a = 

                  3.140625

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

b = 

                    25.125

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

binary_a =

    '0000001100100100'


binary_b =

    '0001100100100000'
Example 2. Example 2

In the following example, a is a real-valued fi object, and K is a negative integer.

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

a = fi(pi,1,16,8)
b = pow2(a,-4)
binary_a = bin(a)
binary_b = bin(b)
a = 

                  3.140625

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

b = 

                 0.1953125

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

binary_a =

    '0000001100100100'


binary_b =

    '0000000000110010'
Example 3. Example 3

The following example shows the use of pow2 with a complex fi object:

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

                         57 -                     2i
      numerictype(1,16,8)
pow2(a,2)
ans = 

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

Extended Capabilities

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

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced before R2006a