位移X *一个数字 [英] Bit shifting x * a number
问题描述
你怎么样 -10
数从这些位转移的实践问题?
据我了解 X * 32
可以写成 X<< 5
。但是你怎么让喜欢数x * 66
或 X *( - 10)
?
常规说明
位移动是主要目的转向数字的二进制重新presentation。它的不可以乘法。
23 = 0001 0111
23℃;&下; 1 = 0001 0111 LT;< 1 = 0010 = 1110 46
然而,由于一个数的二进制重新presentation被改变,则
数它重新presents也发生了变化。这是计算机的二进制系统是如何工作的。因而人们有时会利用这种行为为黑客,主要是为了加快计算时间。
块引用>让我们试着去了解它更多:
左位移位和右位移
现在,当重新presented数量
整数
输入,然后通过1移一个数的二进制重新presentation到左将等同于乘法就以2:23 = 0001 0111
23℃;&下; 1 = 0001 0111 LT;< 1 = 0010 = 1110 // 46左1位移,数变为一倍由于有没有溢出作为给定的数据类型:
255 = 1111 1111 //假设8位数据类型
255 LT;< 1 = 1111 1111 LT;< 1 = 1111 1110 = 254 //不乘以2,因为溢出,虽然转移整数的右侧将相当于为分它由2,然后四舍五入下来
23 = 0001 0111
23 GT;> 1 = 0001 0111 GT;> 1 = 000 1011 = 11 //右移1位移,数变为减半,舍去有些使用,并链接到乘除法
由于位换档操作通常比乘法成本更低,加快速度,你会在一些项目看,人用左位移位操作(如更换乘法)时,他们的意思<$乘它C $ C> 2 (即2,4,8,16等)功率的整数:
int类型的= 23;
...
A = A&所述;&下; 2; // = 102;乘以4,当量为a = A * 4,但更快的操作或者使用权的比特移位操作(作为替代除法和向下舍入的),以将其与
划分的功率的整数2
(也就是2,4 ,8,16,等)int类型的= 23;
...
A = A&GT;&GT; 2; // = 5;除以4,并四舍五入,相当于整数除法A = A / 4,但速度更快结束语
请注意,只有当你用数字与2电源运行,所有的乘法和上述部门可以通过左位移位或右侧位移来代替。
在你的榜样,66和-10不是整数,其中2的功率,所以你不能黑客二进制移操作乘法/除法。
在一般情况下,使用位移位,如果你意味着位转移,如位移有许多其他用途的不仅仅是黑客的乘法/除法与2的幂的整数如果你想乘操作或鸿沟,很乐意与只用乘法(
*
)或除法(/
)运算符。一些附加备注:
话虽这么说,我只是想补充有关的位移进一步解释一些更多的东西(它不会伤害):
- 签署
整数
键入可以保持正或负号- 有逻辑比特移位和具有负号处理时算术位移之间的差。一会给
0
在清空空间后移,而另一会给1
- 因此,它可能是最好的注意,位移位主要用于
无符号
类型,比如像点点转移创造位掩码。也就是说,无符号
建议用于避免符号扩展惊喜,当你处理负数(右)位移位。How do you get number like
-10
from these bit shifting practice problems?From what I understand
X*32
can be written asx<<5
. But how are you to get numbers likex*66
, orX*(-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: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:
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):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)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.
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.
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.
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- 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 give1
- 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. That is,unsigned
is recommended to be used to avoid sign-extension surprises when you deal with negative number (right) bit-shift.这篇关于位移X *一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!