Arthur J. O'Dwyer said:
Sort-of-correct. At the machine level, the mathematical concept of
"zero" can be represented by any number of bit patterns. Sure, it's
common (these days) to represent "zero" as 32 contiguous bits aligned
in a particular way and all set to the low state; but "zero" on some
machines might be 24 bits with the high-order 6 bits all set to 1, for
parity checking; or something even more unconventional.
Bottom line: All-bits-zero does *not* mean value-zero. One
concept is much lower-level than the other. Avoid 'memset'
and 'calloc' for now, and I think you'll be okay.
This came up about a week ago, in the context of using memset() to
zero an array, and Jack Klein posted this:
[snip]
I'm not sure about his conclusion, though. I don't see anything in
the quote that says zero has to be represented by all zero bits. It
has to be a one-to-one mapping, but theoretically all zero bits
could represent something else and zero could be represented by
something else.
For the uint_XXt types defined in C99, yes, all-zero-bits means
"zero". [That's because unsigned types are guaranteed to be
represented in a binary notation; see N869, sec. 6.2.6.2#1.]
This is *not* true for signed types (consider ones'-complement),
the normal unsigned types (consider padding bits), pointers (well
duh), floating-point types (well duh), or whatever I've missed
in that list.
All-zero-bits does mean "zero" for 'unsigned char', and for those
special C99 types. But not in general.
HTCTU,
-Arthur
The are exactly two and only two situations in C where any integer
type with a value of 0 can contain non-zero bits.
1. The representation has padding bits, in which case one or more of
the padding bits might be non-zero.
2. The implementation uses 1's complement or signed magnitude
representation for a signed integer type and that signed integer type
contains the value of -0. Note that -0 may be a trap representation
on such implementations rather than a valid value.
The meaning from the standard is quite unmistakable:
========
6.2.6.2 Integer types
1 For unsigned integer types other than unsigned char, the bits of the
object representation shall be divided into two groups: value bits and
padding bits (there need not be any of the latter). If there are N
value bits, each bit shall represent a different power of 2 between 1
and 2N-1, so that objects of that type shall be capable of
representing values from 0 to 2N - 1 using a pure binary
representation; this shall be known as the value representation. The
values of any padding bits are unspecified.
========
The quotation above clearly states that the one and only possible
representation for 0 in any unsigned integer type must be all value
bits 0.
========
2 For signed integer types, the bits of the object representation
shall be divided into three groups: value bits, padding bits, and the
sign bit. There need not be any padding bits; there shall be exactly
one sign bit. Each bit that is a value bit shall have the same value
as the same bit in the object representation of the corresponding
unsigned type (if there are M value bits in the signed type and N in
the unsigned type, then M <= N). If the sign bit is zero, it shall not
affect the resulting value. If the sign bit is one, the value shall be
modified in one of the following ways:
— the corresponding value with sign bit 0 is negated (sign and
magnitude);
— the sign bit has the value -(2N) (two’s complement);
— the sign bit has the value -(2N - 1) (one’s complement).
Which of these applies is implementation-defined, as is whether the
value with sign bit 1 and all value bits zero (for the first two), or
with sign bit and all value bits 1 (for one’s complement), is a trap
representation or a normal value. In the case of sign and
magnitude and one’s complement, if this representation is a normal
value it is called a negative zero.
========
....and the second quotation clearly states that any signed integer
type with a positive value must have the same bit representation as
the corresponding unsigned type in its value bits.
So any signed or unsigned integer type with all its value bits 0 has
the value of 0. A signed integer type with a value of 0 (as opposed
to -0, on non 2's complement systems if they support -0) must also
have its sign bit 0.
So only padding bits, if any, may be non-zero in the representation of
any C integer type with a value of 0.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq