For the following code:
short shortArray [] = { ( 'B' << 8 ) + 'A', ( 'D' << 8 ) + 'C', ( 'F' << 8 ) +
'E', 'G' };
cout << (char*)shortArray;
The output is:
ABCDEFG
Can someone explain to me the way in which this works?
The expression ( 'B' << 8 ) + 'A'
has a result of type int
, will be equal to 'B'*256 + 'A'
.
When converted to a short
(assuming a short
is represented as two 8-bit bytes) this value is represented by the pair of bytes AB
, where the A
is the least significant byte. On a little-endian machine, the least significant byte occurs first in memory (i.e. the 'A'
is left-most, and the next byte is 'B'
). On a big-endian machine, the 'B'
will be left-most.
The output you describe suggests your host architecture is little-endian.
Intel processors are all little-endian. Historically, a range of CPUS including Motorola 68000 series processors, PowerPC, and Sparc (from Sun Microsystems) were all big-endian. Network byte order (used to communicate data over a network) is big-endian.
Note: for sake of discussion, I'm ignoring the fact your code as shown has undefined behaviour. The streaming operator <<
which accepts a char *
ASSUMES the presence of a terminating char
with value '\0'
. Your code does not ensure that terminator is present.
A short is 16 bits and a char is 8. So one short can hold two characters in it.
The ( 'B' << 8 ) + 'A'
portion is shifting the value of 'B' by 8 bits and adding it to A. So now each half of the short is holding the ASCII code of a different character. In this case A and B.
Casting it to char* now makes it so the compiler will interpret the array as a string (which is just an array of characters) and you get that output
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With