右移2执行鸿沟在-1 [英] Right Shift to Perform Divide by 2 On -1

查看:85
本文介绍了右移2执行鸿沟在-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以通过2使用右移执行鸿沟。

I know that I can perform divide by 2 using right shift.

为了简单起见,取4位数字系统

For simplicity, take a 4 bit number system

-1 - 1111
-2 - 1110
-3 - 1101
-4 - 1100
-5 - 1011
-6 - 1010
-7 - 1001
-8 - 1000
7  - 0111
6  - 0110
5  - 0101
4  - 0100
3  - 0011
2  - 0010
1  - 0001
0  - 0000

如果我试图执行

6 / 2 = 0110 >> 1 = 0011 = 3
-6/ 2 = 1010 >> 1 = 1101 = -3

都有效+ ve和-ve号

Is valid for both +ve and -ve number

然而,当走到1

1 / 2 = 0001 >> 1 = 0000 = 0
-1/ 2 = 1111 >> 1 = 1111 = -1

好像有在-1一个特殊的情况下,右移,然后将其移动到负无穷大。

Seems like there is a special case in -1, as right shift then to move it to negative infinity.

目前,我需要把一个特殊的,如果检查这一点,因为我期待-1 / 2 = 0。

Currently, I need to put a special if check for this, as I am expecting -1 / 2 = 0.

我想知道你是怎么家伙处理此异常在code?你小子放一个,如果检查?

I was wondering how do you guy handle this exception in your code? You guy put an if check?

推荐答案

任何负面的奇数将无法正常工作。然而,为了回答你的问题,如果你知道你可以有负数,只是2.本变成了修正转向由JIT /编译器划分。

Any negative odd number won't work. However to answer your question, if you know you can have negative numbers, just divide by 2. This is turned into a shift with a fixup by the jit/compiler.

这篇关于右移2执行鸿沟在-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆