主要内容

pow2

Efficient fixed-point multiplication by 2K

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.

For floating-point numbers, use the pow2 function.

example

Examples

collapse all

This example shows how to scale real-valued fixed-point significands by 2 raised to the power of exponent.

Positive Integer-Valued Exponent

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

a = fi(pi,1,16,8)
a = 
    3.1406

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 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 23.

binary_a = bin(a)
binary_a = 
'0000001100100100'
binary_b = bin(b)
binary_b = 
'0001100100100000'

Negative Integer-Valued Exponent

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

a = fi(pi,1,16,8)
a = 
    3.1406

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 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 4 places to the right, effectively multiplying a by 2-4.

binary_a = bin(a)
binary_a = 
'0000001100100100'
binary_b = bin(b)
binary_b = 
'0000000000110010'

This example shows how to use the pow2 function 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)

Input Arguments

collapse all

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

fi objects of data type Boolean are not supported.

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, the pow2 function operates on both the real and complex portions of a.

Data Types: fi
Complex Number Support: Yes

Exponent value, specified as a scalar.

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

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

Limitations

  • The syntax b = pow2(a) is not supported when a is a fi object.

Tips

  • 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, try using the bitshift function.

Extended Capabilities

expand all

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

See Also

| | | |