tags:

views:

102

answers:

3
struct in_addr ipv4;

ipv4.s_addr = (uint32_t)(-0)
A: 

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.

Slokun
This is only true for one's complement.
Oli Charlesworth
+6  A: 

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.

Jack
Any architecture will result in -0 cast to an unsigned int being 0x00000000.
Dingo
+1  A: 

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.

Jens Gustedt