Skip to Main Content Skip to Search
Product Documentation

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.

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 OverflowMode and RoundMode properties associated with a. If obeying the RoundMode 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

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)

MATLAB returns:

a =
 
    3.1406

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

b =
 
   25.1250

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

binary_a =

0000001100100100


binary_b =

0001100100100000

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)

MATLAB returns:

a =
 
    3.1406

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

b =
 
    0.1953

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

binary_a =

0000001100100100


binary_b =

0000000000110010

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
      s16,8

pow2(a, 2) 

ans =
               127.99609375 -                     8i
      s16,8

See Also

bitshift | bitsll | bitsra | bitsrl

  


Free Early Verification Kit

Learn how to apply early verification to your development process through these technical resources.

How much time do you spend on testing to ensure implementation meets system-level requirements?

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS