matlab的bitshift,Shift bits specified number of places

bitshift

Shift bits specified number of places

Syntax

c = bitshift(a, k)

Description

c = bitshift(a, k) returns the value of a shifted

by k bits. The input fi object a may

be a scalar value or a vector and can be any fixed-point numeric type.

The output fi object c has the

same numeric type as a. k must

be a scalar value and a MATLAB® built-in numeric type.

The OverflowAction property of a is

obeyed, but the RoundingMethod is always Floor.

If obeying the RoundingMethod property of a is

important, try using the pow2 function.

When the overflow action is Saturate the

sign bit is always preserved. The sign bit is also preserved when

the overflow action is Wrap, and k is

negative. When the overflow action is Wrap and k is

positive, the sign bit is not preserved.

When k is positive, 0-valued bits

are shifted in on the right.

When k is negative, and a is

unsigned, or a signed and positive fi object, 0-valued

bits are shifted in on the left.

When k is negative and a is

a signed and negative fi object, 1-valued bits

are shifted in on the left.

Examples

This example highlights how changing the OverflowAction property

of the fimath object can change the results returned

by the bitshift function. Consider the following

signed fixed-point fi object with a value of 3,

word length 16, and fraction length 0:

a = fi(3,1,16,0);By

default, the OverflowAction fimath property

is Saturate. When a is shifted

such that it overflows, it is saturated to the maximum possible value:

for k=0:16,b=bitshift(a,k);...

disp([num2str(k,'%02d'),'. ',bin(b)]);end

00. 0000000000000011

01. 0000000000000110

02. 0000000000001100

03. 0000000000011000

04. 0000000000110000

05. 0000000001100000

06. 0000000011000000

07. 0000000110000000

08. 0000001100000000

09. 0000011000000000

10. 0000110000000000

11. 0001100000000000

12. 0011000000000000

13. 0110000000000000

14. 0111111111111111

15. 0111111111111111

16. 0111111111111111Now change OverflowAction to Wrap.

In this case, most significant bits shift off the “top”

of a until the value is zero:

a = fi(3,1,16,0,'OverflowAction','Wrap');

for k=0:16,b=bitshift(a,k);...

disp([num2str(k,'%02d'),'. ',bin(b)]);end

00. 0000000000000011

01. 0000000000000110

02. 0000000000001100

03. 0000000000011000

04. 0000000000110000

05. 0000000001100000

06. 0000000011000000

07. 0000000110000000

08. 0000001100000000

09. 0000011000000000

10. 0000110000000000

11. 0001100000000000

12. 0011000000000000

13. 0110000000000000

14. 1100000000000000

15. 1000000000000000

16. 0000000000000000

Extended Capabilities

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

HDL Code Generation

Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.

For efficient HDL code generation, use bitsll,

bitsrl, or

bitsra instead of bitshift.

Introduced before R2006a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值