x == 0&& (x& -1)!= 0表示负零? [英] x == 0 && (x & -1) != 0 for negative zero?

查看:109
本文介绍了x == 0&& (x& -1)!= 0表示负零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,如果整数x为
负零,则(x& -1)为非零。所以对于签名类型,x == 0不保证

(x& foo)== 0.这是对的吗? (不是我希望永远

遇到一个非二合一机器。只是想知道。)


-

Hallvard

As far as I can tell, (x & -1) is nonzero if the integer x is
negative zero. So for signed types, x == 0 does not guarantee
(x & foo) == 0. Is that right? (Not that I expect to ever
encounter a non-two''s-complement machine. Just wondering.)

--
Hallvard

推荐答案

我写道:

据我所知,如果整数x为
负零,则(x& -1)为非零。
As far as I can tell, (x & -1) is nonzero if the integer x is
negative zero.



呃,无论如何都是一个补码。为了使

符号/幅度变得怪异,我们需要x == 0&& (x ^ 1)< 0.

或(x> 1)!= 0,但这是'实现定义的。

Er, for one''s complement anyway. To get weirdness with
sign/magnitude as well, we''d need x == 0 && (x ^ 1) < 0.
Or (x >1) != 0, but that''s implementation-defined.


因此对于签名类型,x == 0不保证

(x& foo)== 0.这是对的吗? (不是我希望永远

遇到一个非二合一机器。只是想知道。)
So for signed types, x == 0 does not guarantee
(x & foo) == 0. Is that right? (Not that I expect to ever
encounter a non-two''s-complement machine. Just wondering.)



-

Hallvard

--
Hallvard


Hallvard B Furuseth写道:
Hallvard B Furuseth wrote:

我写道:
I wrote:

>据我所知,如果整数x为负零,则(x& -1)为非零。
>As far as I can tell, (x & -1) is nonzero if the integer x is
negative zero.



呃,无论如何都是一个补码。为了使

符号/幅度变得怪异,我们需要x == 0&& (x ^ 1)< 0.

或(x> 1)!= 0,但这是'实现定义的。


Er, for one''s complement anyway. To get weirdness with
sign/magnitude as well, we''d need x == 0 && (x ^ 1) < 0.
Or (x >1) != 0, but that''s implementation-defined.


>所以对于签名类型,x == 0不保证
(x& foo)== 0.是吗? (不是说我希望永远不会遇到一个非二合一的机器。只是想知道。)
>So for signed types, x == 0 does not guarantee
(x & foo) == 0. Is that right? (Not that I expect to ever
encounter a non-two''s-complement machine. Just wondering.)



为什么你会对签名操作数使用按位运算?按位

逻辑意味着你有一些位图,自然(和C)模型

因为它是无符号类型。 [我想你的意思是(未签名)-1。]

- 方舟

Why would you ever use bitwise operations on signed operands? Bitwise
logic implies that you''ve got some bitmap, the natural (and the C) model
for it is an unsigned type. [And I suppose you meant (unsigned)-1.]
-- Ark


Ark Khasin写道:
Ark Khasin writes:

> Hallvard B Furuseth写道:
>Hallvard B Furuseth wrote:

>>我写道:
>>I wrote:

>>据我所知,如果整数x为负零,则(x& -1)为非零。
>>As far as I can tell, (x & -1) is nonzero if the integer x is
negative zero.


呃,无论如何都是一个人的补充。为了使
符号/幅度变得奇怪,我们需要x == 0&& (x ^ 1)< 0.
或(x> 1)!= 0,但这是'实现定义的。


Er, for one''s complement anyway. To get weirdness with
sign/magnitude as well, we''d need x == 0 && (x ^ 1) < 0.
Or (x >1) != 0, but that''s implementation-defined.


>>因此对于签名类型,x == 0不保证
(x& foo)== 0.是吗? (不是说我希望永远不会遇到一个非二合一机器。只是想知道。)
>>So for signed types, x == 0 does not guarantee
(x & foo) == 0. Is that right? (Not that I expect to ever
encounter a non-two''s-complement machine. Just wondering.)



你为什么要对签名操作数使用按位运算?按位

逻辑意味着你有一些位图,自然(和C)模型

因为它是无符号类型。


Why would you ever use bitwise operations on signed operands? Bitwise
logic implies that you''ve got some bitmap, the natural (and the C) model
for it is an unsigned type.



按位逻辑意味着按位操作目前很有用。

可能仅适用于例如''& 15''而不是''/ 16''。


对有符号整数的位操作很常见,例如如果有人知道

,那么只使用了最不重要的位数。


另外,变量的类型选择取决于它的所有方式使用了
,包括用于传递它的API,它是如何读取/写入的b $ b $以及它将遇到的其他整数类型。

Bitwise logic implies that bitwise operations are useful at the moment.
Could be just for e.g. ''a & 15'' instead of ''a / 16''.

Bit operations on signed integers are common enough, e.g. if one knows
that only a fwe of the least siginificant bits are used.

Also the choice of type for a variable depends on all the ways it''s
used, including things like the API for passing it, how it''s
read/written, and what other integer types it will meet.


[我想你的意思是(无符号)-1。]
[And I suppose you meant (unsigned)-1.]



否,(无符号)-1是没有签名。


-

Hallvard

No, (unsigned)-1 is not signed.

--
Hallvard


这篇关于x == 0&amp;&amp; (x&amp; -1)!= 0表示负零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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