在硬件语言 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为正