ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[原创]Matlab 之按位操作

2019-09-03 20:04:06  阅读:581  来源: 互联网

标签:位操作 原创 bitshift bitand 按位 Matlab integer bits


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

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

按位左右移 bitshift

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 为负值表示向右移;示例如下有:

>> bitshift(5,1)

ans =

    10

>> bitshift(5,-1)

ans =

     2

按位与 bitand

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').

两个简单的示例如下:

>> bitand(5,4)

ans =

     4

>> bitand(5,15)

ans =

     5

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

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

标签:位操作,原创,bitshift,bitand,按位,Matlab,integer,bits
来源: https://www.cnblogs.com/airbird/p/11455187.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有