简体   繁体   English

位移x *一个数字

[英]Bit shifting x * a number

How do you get number like -10 from these bit shifting practice problems? 如何从这些位移实践问题中获得-10数字?

From what I understand X*32 can be written as x<<5 . 根据我的理解, X*32可以写成x<<5 But how are you to get numbers like x*66 , or X*(-10) ? 但你怎么得到像x*66X*(-10)

General Explanation 一般说明

Bit shifting is primarily aimed to shift the binary representation of a number. 位移主要旨在移动数字的二进制表示。 It is not for multiplication. 不是为了乘法。

23 = 0001 0111
23 << 1 = 0001 0111 << 1 = 0010 1110 = 46

However, as the binary representation of a number is changed, the number it represents is also changed. 但是,随着数字的二进制表示改变,它所代表的数字也会改变。 This is just how computer binary system works. 这就是计算机二进制系统的工作原理。 And thus people sometimes exploit this behavior as a "hack", mostly to speed up the computation time. 因此人们有时会利用这种行为作为“黑客”,主要是为了加快计算时间。

Let's try to understand it more: 让我们试着更多地了解它:


Left bit-shift and right bit-shift 左移位和右移位

Now, when the number represented is of integer type, then shifting the binary representation of a number by 1 to the left will be equivalent to multiplying it by 2: 现在,当表示的数字是integer类型时,将数字的二进制表示向左移动将等于乘以 2:

23 = 0001 0111
23 << 1 = 0001 0111 << 1 = 0010 1110 = 46 //left bit-shift by 1, number becomes doubled

Given that there is no overflow for the given data type: 鉴于给定数据类型没有溢出

255 = 1111 1111 //assuming 8-bit data type
255 << 1 = 1111 1111 << 1 = 1111 1110 = 254 //not multiplied by 2, because of overflow

While shifting integer number to the right will be equivalent as dividing it by 2 and then rounding it down : 向右移动整数将等同于其除以2然后向下舍入

23 = 0001 0111
23 >> 1 = 0001 0111 >> 1 = 000 1011 = 11 //right bit-shift by 1, number becomes halved, rounded down

Some use and link to multiplication and division 有些使用并链接到乘法和除法

Since bit-shifting operation is typically less costly than multiplication, to speed things up, you will see in some program, people use left bit-shift operation (as a replacement of multiplication) when they mean to multiply it by an integer number of power of 2 (that is 2, 4, 8, 16, etc): 由于位移操作通常比乘法成本低,为了加快速度,你会在某些程序中看到,人们使用左位移操作(作为乘法的替代),当它们意味着乘以an integer number of power of 2 (即2,4,8,16等):

int a = 23;
...
a = a << 2; //=102; multiply by 4, equivalent to a = a * 4, but faster operation

Or use right bit-shift operation (as a replacement of division and rounding down) to divide it with an integer number of power of 2 (that is 2, 4, 8, 16, etc) 或者使用正确的位移操作(作为除法和舍入的替代)将其除以an integer number of power of 2 (即2,4,8,16等)

int a = 23;
...
a = a >> 2; //=5; divide by 4 and rounding down, equivalent to integer division a = a / 4, but faster

Concluding remarks 结束语

Note that only if you operate with number with power of 2, all the multiplications and divisions above can be replaced by left bit-shift or right bit-shift. 请注意,只有使用幂为2的数字进行操作时,上面的所有乘法和除法才能被左移位或右移位替换。

In your example, 66 and -10 are not integer number which of power of 2, thus you cannot "hack" the multiplication/division with binary-shifting operation. 在你的例子中,66和-10不是2的幂的整数,因此你不能通过二进制移位操作“破解”乘法/除法。

In general, use bit-shift operation if you mean for bit-shifting, as bit-shifting has many other uses than just "hacking" for multiplication/division with integer number of power of 2. If you want to multiply or divide, be happy with just using multiplication ( * ) or division ( / ) operator. 一般情况下,如果你的意思是位移,则使用位移操作,因为位移具有许多其他用途,而不仅仅是“黑客”乘法/除法,整数倍的2的幂。如果你想乘法或除法,很高兴只使用乘法( * )或除法( / )运算符。


Some additional remarks: 一些补充说明:

That being said, I would just like to add some more things regarding the bit-shift for further explanation (it won't do harm): 话虽这么说,我只想补充一些关于位移的事情,以便进一步解释(它不会造成伤害):

  • signed integer type can hold positive or negative number 有符号integer类型可以保存正数或负数
  • there is a difference between logical bit-shift and arithmetic bit-shift when dealing with negative number. 处理负数时,逻辑位移和算术位移之间存在差异。 One will give 0 in the emptied-space after shift while the other will give 1 一个人在转移后在空旷空间给0 ,而另一个将给出1
  • Hence, it probably best to note that the bit-shift is mainly used for unsigned type, such like for creating bit-masks by bit shifting. 因此,最好注意位移主要用于unsigned类型,例如通过位移创建位掩码。 That is, unsigned is recommended to be used to avoid sign-extension surprises when you deal with negative number (right) bit-shift. 也就是说,当您处理负数(右)位移时,建议使用unsigned来避免符号扩展意外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM