开发者

How is negative zero used in this context?

struct in_addr ipv开发者_开发百科4;

ipv4.s_addr = (uint32_t)(-0)


That's strange, usually arithmetic is implemented through Two's Complement so you don't effectively have a negative representation of 0.

In a One's Complement version the negative 0 is instead stored as 0xFFFFFFFF so you would have 255.255.255.255 when converted to an ipv4 address after the cast to unsigned int.

While in a normal architecture using -0 to cast it to an unsigned int should just give you 0x00000000.


This makes not much sense, since the standard says about the exact width integer types

These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names.

So first of all there is no negative zero for any of these signed types and then even less for the unsigned ones. In any case for the assignment that you show a simple 0 would be as good, or UINT32_C(0) if you want to be picky.


It would be used to get the signed value of the unsigned integer, or, one more than the maximum value of a signed integer.

The uint32_t would be casting it to an unsigned integer, while the -0 would be forcing it to be negative, flipping the signed bit, and returning the unsigned value with it flipped.

This article explains signed integers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜