Main Content

位操作

该主题显示了如何在MATLAB®中使用位智能操作来操纵数字的位。大多数现代CPU都直接支持在位上操作。万博1manbetx在许多情况下,以这种方式处理数字的位比执行算术操作(如除法或乘法)要快。

number Representations

任何数字都可以用位代表(也称为binary数字)。数字的二进制形式或碱基2包含1s和0s,以指示数字中存在哪个功率。例如,7位二进制形式的7是

00000111

一组8位也称为1经过te。In binary representations, the bits are counted from the right to the left, so the first bit in this representation is a 1. This number represents 7 because

2 2 + 2 1 + 2 0 = 7

当您将数字键入MATLAB时,它假设数字为双精度(64位二进制表示)。但是,您还可以指定单位数字(32位二进制表示)和整数(签名或未签名,从8到64位)。例如,存储数字7的最有效方法是使用8位未签名整数:

a = uint8(7)
a =UINT87

您甚至可以使用前缀直接指定二进制表单0bfollowed by the binary digits (for more information, see十六进制和二进制值)。MATLAB以数量最少的位数以整数格式存储数字。您不需要指定所有位,而是只需指定最左边的1和右侧的所有数字。该位左侧的位在微不足道上为零。所以数字7是:

b =0b111
b =UINT87

Matlab使用两个补充来存储负整数。例如,考虑8位签名的整数-8。要找到这两个数字的补充位模式:

  1. 从数字的正版本的位模式开始,8:00001000

  2. next, flip all of the bits:11110111

  3. 最后,将1添加到结果:11111000

这result,11111000, is the bit pattern for -8:

n=0b11111000s8
n=int8-8

MATLAB不本质地显示数字的二进制格式。为此,您可以使用十二月2bin函数,返回正整数的二进制数字的字符向量。同样,此函数仅返回并非微不足道的数字。

dec2bin(b)
ans ='111'

You can usebin2decto switch between the two formats. For example, you can convert the binary digits10110101to decimal format with the commands

data = [1 0 1 1 0 1 0 1]; dec = bin2dec(num2str(data))
dec =181

cast打字functions are also useful to switch among different data types. These functions are similar, but they differ in how they treat the underlying storage of the number:

  • cast— Changes the underlying data type of a variable.

  • 打字- 转换数据类型而不更改基础位。

Because MATLAB does not display the digits of a binary number directly, you must pay attention to data types when you work with bit-wise operations. Some functions return binary digits as a character vector (十二月2bin), some return the decimal number (bitand),其他人返回零件本身的向量()。

逻辑操作员的位掩盖

MATLAB has several functions that enable you to perform logical operations on the bits of two equal-length binary representations of numbers, known asbit masking

  • bitand- 如果both数字are 1, then the resulting digit is also a 1. Otherwise, the resulting digit is 0.

  • 比尔- 如果任何一个数字为1,然后由此产生的数字也为1。否则,所得数字为0。

  • bitxor- 如果数字不同,则结果数字为1。否则,结果数字为0。

除这些功能外,位的补充还可以BITCMP,但这是一个单一的操作,一次仅在一个数字上翻转位。

One use of bit masking is to query the status of a particular bit. For example, if you use a bit-wise AND operation with the binary number00001000,您可以查询第四位的状态。然后,您可以将该位移至第一个位置,以便MATLAB返回0或1(下一部分描述了更详细的位移动)。

n=0b10111001; n4 = bitand(n,0b1000); n4 = bitshift(n4,-3)
N4 =UINT81

位的操作可能会有令人惊讶的应用程序。例如,考虑数字的8位二进制表示 n = 8

00001000

8is a power of 2, so its binary representation contains a single 1. Now consider the number (( n - 1 = 7

00000111

通过减去1,从最正确的1开始的所有位都是翻转的。结果,当 n is a power of 2, corresponding digits of n (( n - 1 are always different, and the bit-wise AND returns zero.

n = 0b1000;bitand(n,n-1)
ans =UINT80

但是,什么时候 n is not a power of 2, then the right-most 1 is for the 2 0 有点,所以 n (( n - 1 have all the same bits except for the 2 0 少量。对于这种情况,位并返回非零数字。

n=0b101; bitand(n,n-1)
ans =UINT84

此操作建议一个简单的功能,该功能在给定输入号码的位上运行,以检查数字是否为2的功率:

functiontf = isPowerOfTwo(n) tf = n && ~bitand(n,n-1);结尾

这use of the short-circuit AND operator&&检查以确保n不是零。如果是这样,则该函数无需计算bitand(n,n-1)to know that the correct answer isfalse

Shifting Bits

Because bit-wise logical operations compare corresponding bits in two numbers, it is useful to be able to move the bits around to change which bits are compared. You can usebitshiftto perform this operation:

  • bitshift((一个,N)移动一个to the剩下经过n数字。This is equivalent to繁殖一个经过 2 n

  • bitshift((一个,-N)移动一个to the正确的经过n数字。This is equivalent todividing一个经过 2 n

这些操作有时写一个<(左移)和a >> n(右班),但MATLAB不使用<<>>为此目的运营商。

当数字的位移动时,一些位从数字末尾掉下来,并且0S或1s are introduced to fill in the newly created space. When you shift bits to the left, the bits are filled in on the right; when you shift bits to the right, the bits are filled in on the left.

例如,如果您移动数字8的位(二进制:1000)在右边,您可以获得4(二进制:100)。

n = 0b1000;bitshift((n,-1)
ans =UINT84

同样,如果您移动数字15(二进制:1111)to the left by two digits, you get 60 (binary:111100)。

n = 0b1111;Bitshift(15,2)
ANS = 60

When you shift the bits of a negative number,bitshift保留签名位。例如,如果您移动签名的整数-3(二进制:11111101)在右边的2位数中,您获得-1(二进制:11111111)。在这些情况下,bitshift在左边填充1S而不是0s。

n=0b11111101s8; bitshift(n,-2)
ans =int8-1

写作

您可以使用bitsetfunction to change the bits in a number. For example, change the first bit of the number 8 to a 1 (which adds 1 to the number):

bitset(8,1)
ANS = 9

By default,bitset翻转碎片or 1. You can optionally use the third input argument to specify the bit value.

bitsetdoes not change multiple bits at once, so you need to use a为了循环更改多个位。因此,您更改的位可以是连续的,也可以是非连续的。例如,更改二进制数的前两个位1000

位= [1 2];C = 0B1000;为了k = 1:numel(bits)c = bitset(c,bits(k));结尾十二月2bin(c)
ans = '1011'

一个nother common use ofbitsetis to convert a vector of binary digits into decimal format. For example, use a loop to set the individual bits of the integer11001101

data = [1 1 0 0 1 1 0 1]; n = length(data); dec = 0b0u8;为了k = 1:n dec = bitset(dec,n+1-k,data(k));结尾十二月
dec =UINT8205
DEC2BIN(DEC)
ans ='11001101'

Reading Consecutive Bits

位移动的另一种用途是分离连续的位。例如,在16位编号中读取最后四位0110000010100000。回想一下最后四位在剩下二进制表示。

n = 0B0110000010100000;dec2bin(bithift(n,-12))
ans ='110'

To isolate consecutive bits in the middle of the number, you can combine the use of bit shifting with logical masking. For example, to extract the 13th and 14th bits, you can shift the bits to the right by 12 and then mask the resulting four bits with0011。Because the inputs tobitand必须是相同的整数数据类型,您可以指定0011作为一个未签名的16位整数0b11u16。没有-U16suffix, MATLAB stores the number as an unsigned 8-bit integer.

m = 0b11u16; dec2bin(bitand(bitshift(n,-12),m))
ans ='10'

一个nother way to read consecutive bits is with, which reads specified bits from a number. You can use colon notation to specify several consecutive bits to read. For example, read the last 8 bits ofn

咬((n,16:-1:8)
ans =1x9 UINT16行矢量011000001

读取非连续位

您也可以使用当位不彼此相邻时,从数字读取位。例如,从中阅读第五,第8和14位n

位= [14 8 5];bitget(n,位)
ans =1x3 uint16 row vector110

也可以看看

||||||

相关话题