如何检查有符号整数是否为正数?
使用按位运算符以及我想的加法和减法,如何检查有符号整数是否为正(具体来说,不是负数也不是零)?我确信这个问题的答案非常简单,但我就是想不出来。
Using bitwise operators and I suppose addition and subtraction, how can I check if a signed integer is positive (specifically, not negative and not zero)? I'm sure the answer to this is very simple, but it's just not coming to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您确实想要
int n
的“严格为正”谓词而不使用条件(假设 2 的补码):-n
将设置符号(顶部)位,如果 < code>n 严格为正数,并且在所有其他情况下都是明确的,除了n == INT_MIN
;n
严格为正数或0,则~n
将设置符号位,并在所有其他情况下清除包括n = = INT_MIN
;-n &如果 n 严格为正,则 ~n
将设置符号位,并在所有其他情况下清除。应用无符号移位将其变成 0 / 1 答案:
编辑:正如 caf 在评论中指出的那样,当
n == 时
(仍然假设 2 的补码)。 C 标准允许程序在这种情况下失败(例如,您可以使用带有-n
会导致溢出INT_MIN-ftrapv
选项的 GCC 启用有符号溢出陷阱)。将n
转换为 unsigned 可解决该问题(无符号算术不会导致溢出)。因此,改进将是:If you really want an "is strictly positive" predicate for
int n
without using conditionals (assuming 2's complement):-n
will have the sign (top) bit set ifn
was strictly positive, and clear in all other cases exceptn == INT_MIN
;~n
will have the sign bit set ifn
was strictly positive, or 0, and clear in all other cases includingn == INT_MIN
;-n & ~n
will have the sign bit set if n was strictly positive, and clear in all other cases.Apply an unsigned shift to turn this into a 0 / 1 answer:
EDIT: as caf points out in the comments,
-n
causes an overflow whenn == INT_MIN
(still assuming 2's complement). The C standard allows the program to fail in this case (for example, you can enable traps for signed overflow using GCC with the-ftrapv
option). Castingn
to unsigned fixes the problem (unsigned arithmetic does not cause overflows). So an improvement would be:检查最高有效位。 0 为正,1 为负。
Check the most significant bit. 0 is positive, 1 is negative.
如果你不能使用明显的比较运算符,那么你必须更加努力:
第一项检查值是否不为零;第二个检查该值是否没有设置前导位。这应该适用于 2 的补码、1 的补码或符号数值整数。
If you can't use the obvious comparison operators, then you have to work harder:
The first term checks that the value is not zero; the second checks that the value does not have the leading bit set. That should work for 2's-complement, 1's-complement or sign-magnitude integers.
考虑如何表示符号。通常它是用补码或一个简单的符号位来完成的 - 我认为这两者都可以用简单的逻辑与来检查。
Consider how the signedness is represented. Often it's done with two's-complement or with a simple sign bit - I think both of these could be checked with a simple logical and.
检查它不为 0 并且最高有效位为 0,如下所示:
Check that is not 0 and the most significant bit is 0, something like: