> I got all three wrong.
You got the first two right at the second attempt and the third one right after that.
So, here is what we know so far:
a. Integral types have values within a range: a minimum possible value and a maximum possible value
b. Unsigned integral types do not overflow; if we try to give them a value outside the range, they are rounded modulo.
c. If we try to give a value outside its range to a signed integral type, overflow takes place and the result is undefined.
1 2 3 4 5 6
|
// default int is signed int
int i = INT_MAX ; // largest value that an int can hold
++i ; // overflow: undefined behaviour
i = INT_MIN ;
--i ; // undefined behaviour
| |
d. Every bit in the object representation may not participate in the value representation (except for char types).
Therefore, every possible bit pattern of the object representation may not have a valid value representation.
1 2
|
int i ; // uninitialised; the bits in the object representation of i may not hold a valid value representation.
std::cout << i ; // undefined behaviour
| |
e. When we write a four-byte unsigned integer as hexadecimal
0x01020304, with byte being an octet of 8 bits,
the value of the most significant byte is 0x01 (1)
the value of the least significant byte is 0x04 (4)
the value of the unsigned integer is
1 . 2563 + 2 . 2562 + 3 . 256 + 4
f. There is obviously more to bits and bytes (which we will ignore for the present):
are there restrictions on the memory addresses at which an object may be placed? (alignment)
in memory, would the most significant byte have a higher address than the least significant byte? (endianness)
how are negative values represented in a signed integral type? (twos' complement, ones' complement)
how are floating point values represented?
g. In general, while programming we should think of numbers in a logical manner rather than in terms of bits and bytes. To understand that 345 * 3 yields 1035, which is less than 1500, we do not need any special knowledge about bits and bytes.
h. In general, while programming we should think of characters in a logical manner
In particular, it is a beginners' mistake to assume that 'A' will have a value of 65 everywhere.
It is a beginners' mistake to assume that,
char( 'A' + 1 ) would universally yield 'B' because it happens to do so on the implementations one has seen so far.
C++ makes no guarantees about the values of characters other than that the values characters representing decimal digits -
'0', '1', ... '9' - will be contiguous and ascending.
Now, move forward to the next topic in the book.