Two questions on ISO C++

J

john

I want to verify my memory.

0 doesn't necessarily mean all-bits-zero in integrals, and 1 byte is not
necessarily 8 bits (that's why we have numeric_limits<unsigned
char>::digits). Right?
 
V

Victor Bazarov

john said:
I want to verify my memory.

0 doesn't necessarily mean all-bits-zero in integrals, and 1 byte is
not necessarily 8 bits (that's why we have numeric_limits<unsigned
char>::digits). Right?

No, 0 is all bits clear in integrals (all reps acceptable by C++ have
that representation of 0). Yes, 1 byte can have more than 8 bits.

V
 
P

peter koch

I want to verify my memory.

0 doesn't necessarily mean all-bits-zero in integrals, and 1 byte is not
necessarily 8 bits (that's why we have numeric_limits<unsigned
char>::digits). Right?

As Victor wrote, all bits will be zero in integrals, but the null
pointer might not have an all zeroes representation (even though using
0 is perfectly legal (and how I prefer to write "null")).

Peter
 
J

James Kanze

I want to verify my memory.
0 doesn't necessarily mean all-bits-zero in integrals,

Yes and no. The standard requires a "pure binary"
representation for integrals, which means that all value bits 0
must represent the value 0. On the other hand, the standard
allows representations which have both a positive and a negative
0, one of which will not have all bits 0. It also allows
"padding" bits, which may be anything.

So roughly speaking: memset'ing an integral type with 0's
guarantees a zero representation, but the fact memcmp with a lot
of 0 bytes returns non-0 doesn't mean that the value isn't zero.

As far as the standard is concerned, too, the above only holds
for integral types. All bits zero (or memset'ing the object to
0) doesn't guarantee a 0.0 value for floating point, nor a null
pointer for a pointer. (In practice, I've never heard of an
implementation where it didn't work for floating point---perhaps
a Unisys series A. There very definitly have been machines
where it didn't work for pointers, however.)
and 1 byte is not necessarily 8 bits (that's why we have
numeric_limits<unsigned char>::digits). Right?

Right. Standard English usage, that one. A byte has never been
necessarily 8 bits (and in fact, in the original usage at IBM,
was 6 bits). Of course, the C++ standard does use the word in a
somewhat different since, since it allows the size of a byte to
be the same as the size of a word, and requires it to be at
least 8 bits, and that the number of bytes in a word must be an
integer. (Common usage on the PDP-10 was 5 seven bit bytes in a
36 bit word. The byte size was programmable, however, and
presumably any C/C++ implementations used 4 nine bit bytes.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top