Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why, when casting a short[] to char* is the array reversed?

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?

like image 209
Piotrold Avatar asked Jan 22 '18 01:01

Piotrold


2 Answers

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.

like image 155
Peter Avatar answered Sep 19 '22 22:09

Peter


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

like image 42
Mitch Avatar answered Sep 19 '22 22:09

Mitch