Data Structures, Algorithms, & Applications in C++
Chapter 10, Exercise 11
Each char, in C++, is 8 bits.
Therefore, the two left shifts and adds of Program 10.13 essentially
concatenate the binary representations of the rightmost three characters of
s.
Further, since a long is 32
bits and the concatenation of the three characters affects only the
least significant 3 * 8 = 24 bits of
answer, no bits are lost during the left shifts
and there is no overflow during the adds. Therefore, at the end,
the least significant 24 bits of
answer faithfully represent the concatenation
of the 8 bits in each of the three characters
of the input string s.
Consequently, different three-character strings
u and v
are converted into different numbers.