httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Richards <p.richa...@elsevier.co.uk>
Subject Re: cvs commit: apache/src util.c
Date Wed, 13 Nov 1996 11:21:43 GMT
Roy Fielding <fielding@hyperreal.com> writes:

>   !     if (my_addr != (~0ul))

Ahh, no. Ben's right. The function returns -1, since the return type
is unsigned long it'll be -1 cast to an unsigned long. The correct fix
is as Ben said (unsigned long) -1 since this is what the function is
actually returning and we should test for the same construct.

Assuming an 8 bit representation (save me typing lots of 1's).

The representation of ~0ul is 11111111. That's a positive value that is
the 1's complement (which is what the ~ operator is) of 0 i.e. the
largest positive value representable. This is most definately *NOT*
the same as an integral conversion, in fact, an integral conversion
takes place on signed numbers as part of the ~ operator's behaviour.

To quote (integral conversions A6.2 K&RII).

	"Any integer is converted to a given unsigned type by finding
	 the smallest non-negative value that is congruent to that
	 integer, modulo one more than the largest value that can be
	 represented in the unsigned type. In a two's complement
	 represenatation, this is equivalent to left-truncation if the
	 bit pattern of the unsigned type is narrower, and to
	 zero-filling unsigned values and sign-extending if the
	 unsigned type is wider."

Since we're all using 2's complement based machines we're not likely
to see any difference between ~0ul and (unsigned long) -1 but that's
only because we're relying on a particular implementation of the
language and not because ~0ul is correct since it clearly isn't.

-- 
  Paul Richards. Originative Solutions Ltd.  (Netcraft Ltd. contractor)
  Elsevier Science TIS online journal project.
  Email: p.richards@elsevier.co.uk
  Phone: 0370 462071 (Mobile), +44 (0)1865 843155

Mime
View raw message