使用位移位除以 2 的幂
我有以下任务:
使用位移位计算
x/(2^n)
,对于0 <= n <= 30
。要求:向零舍入。
示例:
<前><代码>divpwr2(15,1) = 7 divpwr2(-33,4) = -2合法运营商:
! 〜& ^ | + << >>
操作员最大数量:15
以下是我到目前为止得到的:
public int DivideByPowerOf2(int x, int n)
{
//TODO: find out why DivideByPowerOf2(-33,4) = -3 instead of -2
return x >> n;
}
DivideByPowerOf2(15,1) = 7
没问题。
但 DivideByPowerOf2(-33,4) = -3
而不是 -2。为什么?
I've got the following task:
Compute
x/(2^n)
, for0 <= n <= 30
using bit shifting.Requirement: Round toward zero.
Examples:
divpwr2(15,1) = 7 divpwr2(-33,4) = -2
Legal operators:
! ~ & ^ | + << >>
Maximum number of operators: 15
Here is what I've got so far:
public int DivideByPowerOf2(int x, int n)
{
//TODO: find out why DivideByPowerOf2(-33,4) = -3 instead of -2
return x >> n;
}
DivideByPowerOf2(15,1) = 7
is ok.
But DivideByPowerOf2(-33,4) = -3
instead of -2. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在自己寻找一个好的答案之后,我偶然发现了这个并能够得到一个工作片段。让我帮助向将来可能发现这一点的其他人解释这一点。
这段代码正是您所寻找的,由 Sotelo 发布。它起作用的原因非常重要,尤其是对于您理解作业而言。首先您必须完全理解 2 的补码表示法。这是当最高有效位用于将整个二进制表示偏移相应的 2 次幂时。
如果我们仅对 32 位进行成像(大多数处理器中的标准),那么我们可以使用右移 (>>) 将最高有效位移动到最低有效位。这样做时,您将进行算术右移,这将在整个位级表示中复制最高有效位(如果为负则为 1)。在 6 位二进制表示中,这将导致要么
这然后允许我们进一步对整数进行操作以确定某些属性。首先,我们需要找到 2 的幂,我们要除以(在本例中为 n),并将二进制移位到该位置,然后减去 1。例如,让我们使用 3 或 8 的幂。
现在我们都有 现在,我们将这些二进制表示形式和它们放在一起,
假设 x 是奇数或偶数(分别是情况 1 和情况 2),我们可以将 x 添加到其中并得到一个 2 的完美幂的数字(如果我们除以 2 的幂)我们会得到正确的答案)。下面是 x 分别 = 8、10、-8、-12 的几个示例。
现在最后一步是将这些数字除以 n 次方。除以 8 则为 3,如上所述。
这样你就得到了。您的第一个任务是确定它是负数还是正数,然后从负数中获取与 2 -1 的幂相对应的位。将其添加到 x 中即可得到 2 的可整除数的二进制幂。最后将你的二的幂的右移平分。
After looking for a good answer myself, I stumbled across this and was able to get a working snippet. Let me help explain this to others that may find this in the future.
This snippet of code is what you are looking for as posted by Sotelo. The reason it works though is very important especially for you to understand your homework. First you must understand fully 2's complement representation. This is when the most significant bit is used to offset the entire binary representation by the corresponding power of 2.
If we image just 32 bits (standard in most processors) then we can use a right shift (>>) to move the most significant bit to the least significant bit. In doing so you will do an arithmetic right shift which will copy that most significant bit (a 1 if it is negative) throughout the entire bit level representation. In a 6 bit binary representation this would result in either
This then allows us to further operate on the integer to determine some properties. First we need to find the power of 2 we are going to divide by (in this case n) and shift a binary on to that position, then minus 1. For example let's use power of 3 or 8.
now that we have both of these binary representations we will and them together
now given that x is odd or even (case 1 and case 2 respectively) we can add x to this and get a number which is a perfect power of 2 (if we divide by a power of two we will get a proper answer). Below is a few examples with x = 8, 10, -8, -12 respectively.
Now the final step is to divide these numbers by our power of n. For dividing by 8 this is then 3 as stated above.
So there you have it. You first task is to find if it is negative or positive, then get the bit from the negative that corresponds to your power of 2 -1. Add this to your x to get your power of 2 divisible number in binary. Then lastly divide you a right shift of your power of two.
密切注意舍入行为。
/
(整数除法)始终向零舍入。Pay close attention to the rounding behavior.
/
(integer divide) always rounds toward zero.由于负数的补码表示形式,负数在二进制表示形式中被认为是一次性的。也许阅读二进制补码会有所帮助。
Negative numbers work out to be one off in the binary representation due to their two's complement representation. Perhaps reading about two's complement will help.