Either in 2's complement form or in 1's complement form?
closed as off-topic by Evil, Discrete lizard♦, Yuval Filmus, David Richerby, vonbrand May 16 '18 at 15:38
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question does not appear to be about computer science, within the scope defined in the help center." – Evil, Discrete lizard, Yuval Filmus, vonbrand
The C and C++ standards do not completely specify how integers are stored in memory. C aimed to be platform-independent and at the time of its creation there were a wide variety of different conventions used. C++, being an extension of C, inherited this behavior.
However, nowadays all systems I am aware of store integers in 2's complement form, where the first bit of each number stores the sign.
The C and C++ standards do not completely specify how floating-point numbers are stored in memory. C aimed to be platform-independent and at the time of its creation there were a wide variety of different conventions used. C++, being an extension of C, inherited this behavior.
However, nowadays all systems I am aware of adhere to the IEEE 754 standard (or possibly some minor variation thereof). In particular, the standard describes a bit layout known as binary32 for 32-bit floating point numbers:
- The first bit stores the sign.
- The next eight bits store the exponent $e$ (this can be in the form of either a signed integer or an unsigned but biased integer).
- The next 23 bits store the mantissa $m$, representing the bits of a number in the range $[0,1)$.
In most cases, this represents the value $m\times 2^e$, though there are many exceptions (such as negative zero, signed infinities, and denormalized numbers).
Floating-point arithmetic is very complicated, and the IEEE 754 standard prescribes the exact value that calculations should return, so processors generally handle it correctly. However, attempting to reach into the guts of a floating point number (e.g. via casting a
float* to an
char* and then assigning to its referent) is still undefined behavior in C, meaning that compilers can assume valid programs will never do it for optimization purposes.