[原创]Matlab之按位操作

本文介绍了Matlab中实现按位操作的两种方法:按位左右移bitshift和按位与bitand。按位左右移可通过指定位数实现左移或右移,按位与则用于比较两个数的二进制位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在硬件语言Verilog中按位操作是相对容易的,在C语言中一样的用好逻辑符号“|”、“!”、“&”、“>>”等即可。但是在Matlab中一些类似的操作是判断或者逻辑用法,不能用在按位操作上。那么在其中就需要用到函数来进行操作了。

在此记录两种按位操作的方法:按位左右移bitshift,按位与bitand

按位左右移bitshift

1
2
3
4
5
6
7
8
9
10
11
C = bitshift(A,K) returns the value of A shifted to the left by K bits,
where A is a signed or unsigned integer array. Shifting by K bits
is the same as multiplication by 2^K. Negative values of K are allowed
and this corresponds to shifting to the right, or dividing by 2^ABS(K)
and rounding to the nearest integer towards negative infinity. If the
shift causes C to overflow the number of bits in the integer class of A,
then the overflowing bits are dropped.
 
If A is a double array, then all elements must be non-negative integers
less than or equal to intmax('uint64'), and bitshift
drops any bits overflowing 64 bits.

其中K为正表示向左移,K为负值表示向右移;示例如下有:

1
2
3
4
5
6
7
8
9
10
11
>> bitshift(5,1)
 
ans =
 
10
 
>> bitshift(5,-1)
 
ans =
 
2

 

按位与bitand

1
2
3
4
C = bitand(A,B) returns the bitwise AND of arguments A and B,
where A and B are signed or unsigned integer arrays. If A and B are
double arrays, then they must contain non-negative integer elements
less than or equal to intmax('uint64').

两个简单的示例如下:

1
2
3
4
5
6
7
8
9
10
11
>> bitand(5,4)
 
ans =
 
4
 
>> bitand(5,15)
 
ans =
 
5

 

其他还有一些按位操作的函数,可以参考如下。

See also 
*bitor*, *bitxor*, *bitcmp*, *bitshift*, *bitset*, *bitget*, *intmax*.
 
 

转载于:https://www.cnblogs.com/airbird/p/6160265.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值