使用按位运算符检查数字是正数还是负数
我可以使用按位运算符检查数字是否为奇数/偶数。我可以在不使用任何条件语句/运算符(如 if/三元等)的情况下检查数字是否为正/零/负。
可以使用按位运算符和 C 或 C++ 中的一些技巧来完成同样的操作吗?
I can check whether a number is odd/even using bitwise operators. Can I check whether a number is positive/zero/negative without using any conditional statements/operators like if/ternary etc.
Can the same be done using bitwise operators and some trick in C or in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
当然:
Of course:
如果高位设置为有符号整数(字节、长整数等,但不是浮点数),则该数字为负数。
添加:
您说过您不想使用任何条件。我想你可以这样做:
稍后你可以使用
if (isNegative)
来测试它。If the high bit is set on a signed integer (byte, long, etc., but not a floating point number), that number is negative.
ADDED:
You said that you don't want to use any conditionals. I suppose you could do this:
And at some later time you can test it with
if (isNegative)
.或者,您可以使用
signbit()
工作已经为您完成。我假设在幕后,math.h 实现是一种有效的按位检查(可能解决您最初的目标)。
参考:http://en.cppreference.com/w/cpp/numeric/math /符号位
Or, you could use
signbit()
and the work's done for you.I'm assuming that under the hood, the
math.h
implementation is an efficient bitwise check (possibly solving your original goal).Reference: http://en.cppreference.com/w/cpp/numeric/math/signbit
Bit Twiddling Hacks 页面上有详细的讨论。
There is a detailed discussion on the Bit Twiddling Hacks page.
有符号整数和浮点通常使用最高有效位来存储符号,因此如果您知道大小,则可以从最高有效位提取信息。
这样做通常没有什么好处,因为需要进行某种比较才能使用此信息,并且处理器测试某事物是否为负就像测试它是否不为零一样容易。事实上,在 ARM 处理器上,检查最高有效位通常比预先检查它是否为负值更昂贵。
Signed integers and floating points normally use the most significant bit for storing the sign so if you know the size you could extract the info from the most significant bit.
There is generally little benefit in doing this this since some sort of comparison will need to be made to use this information and it is just as easy for a processor to tests whether something is negative as it is to test whether it is not zero. If fact on ARM processors, checking the most significant bit will be normally MORE expensive than checking whether it is negative up front.
它非常简单,
可以很容易地完成,它
返回
It is quite simple
It can be easily done by
it returns
这不能通过 C 中的位运算以可移植的方式完成。标准允许的有符号整数类型的表示可能比您想象的要奇怪得多。特别是,符号位打开且为零的值不必是有符号类型或无符号类型的允许值,而是这两种类型的所谓陷阱表示。
因此,您可以使用位运算符进行的所有计算都可能会产生导致未定义行为的结果。
在任何情况下,正如其他一些答案所暗示的那样,这并不是真正必要的,与
<
或>
进行比较在任何实际情况下都应该足够了,更高效、更容易阅读...所以就这样做。This can not be done in a portable way with bit operations in C. The representations for signed integer types that the standard allows can be much weirder than you might suspect. In particular the value with sign bit on and otherwise zero need not be a permissible value for the signed type nor the unsigned type, but a so-called trap representation for both types.
All computations with bit operators that you can thus do might have a result that leads to undefined behavior.
In any case as some of the other answers suggest, this is not really necessary and comparison with
<
or>
should suffice in any practical context, is more efficient, easier to read... so just do it that way.这正是您想要的!
Here's exactly what you waht!
这是针对这个老问题的与 C++11 相关的更新。还值得考虑 std::signbit。
在使用 gcc 7.3 64 位和 -O3 优化的编译器资源管理器上,此代码
生成
并且此代码
生成
您需要进行分析以确保存在任何速度差异,但符号位版本确实使用了 1 个较少的操作码。
Here is an update related to C++11 for this old question. It is also worth considering std::signbit.
On Compiler Explorer using gcc 7.3 64bit with -O3 optimization, this code
generates
And this code
generates
You would need to profile to ensure that there is any speed difference, but the signbit version does use 1 less opcode.
当您确定整数的大小时(假设为 16 位 int):
当您不确定整数的大小时:
unsigned
关键字是可选的。When you're sure about the size of an integer (assuming 16-bit int):
When you are unsure of the size of integers:
The
unsigned
keyword is optional.如果值为 0,则数字为正,否则为负
If value is 0 then number is positive else negative
判断数字是正数还是负数的更简单方法:
设数字为x
检查是否 [x * (-1)] > x。如果 true x 为负值,则为正值。
A simpler way to find out if a number is positive or negative:
Let the number be x
check if [x * (-1)] > x. if true x is negative else positive.
您可以通过查看最高有效位来区分负数/非负数。
在有符号整数的所有表示形式中,如果数字为负数,则该位将设置为 1。
除了针对 0 的直接测试之外,没有任何测试可以区分零和正数。
要测试负数,您可以使用
You can differentiate between negative/non-negative by looking at the most significant bit.
In all representations for signed integers, that bit will be set to 1 if the number is negative.
There is no test to differentiate between zero and positive, except for a direct test against 0.
To test for negative, you could use
假设您的数字是
a=10
(正数)。如果您移动a
a
次,则结果为零。即:
因此您可以检查数字是否为正数,但如果
a=-10
(负数):因此您可以将它们组合在
if
中:Suppose your number is
a=10
(positive). If you shifta
a
times it will give zero.i.e:
So you can check if the number is positive, but in case
a=-10
(negative):So you can combine those in an
if
:没有 if:
(不适用于 0)
Without if:
(not working for 0)
它检查第 n 个数字的最高有效位,然后 &如果值为 1(为 true 则该数字为负数),则对其进行操作,否则为正数
It check the first bit which is most significant bit of the n number and then & operation is work on it if the value is 1 which is true then the number is negative and it not then it is positive number