0

This is a very simple question. I noticed that the following, when compiled in MSVS2012, produces the expected result of 0x3412 for val:

unsigned char test[] = { 0x12, 0x34, 0x56, 0x78 };
unsigned char* ch = test;
unsigned int val = *ch | (*(ch+1) << 8);

I would have actually expected the dereferenced char pointer *(ch+1) on the right to produce a char value of 0x34, which would then be shifted left 8 bits producing 0x00. It seems that at the point in time the value is dereferenced, it is already stored in a type large enough to contain at least two bytes.

Is this specified in the C++ standard somewhere? How exactly does this implicit cast happen?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
8bitcartridge
  • 1,629
  • 6
  • 25
  • 38
  • 1
    Actually, a `char` shifted by 8 bits, assuming an 8-bit `char`, would be undefined behaviour if it weren't for the implicit conversion in question. You shouldn't shift something by a number greater than or equal to the number of bits in what you're shifting. – chris Feb 27 '14 at 03:52
  • 1
    If it's implicit, then it's not a cast. A cast is an explicit conversion; this is an implicit conversion (specifically, an integral promotion). – Mike Seymour Feb 27 '14 at 03:58

1 Answers1

4

This is covered under the draft C++ standard section 5.8 Shift operators paragraph 1 which says:

[...]The operands shall be of integral or unscoped enumeration type and integral promotions are performed.[...]

and integral promotions are covered in section 4.5 Integral promotions which in paragraph 1 says:

A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • @8bitcartridge I just put in a link to the draft standard and you can find most of the C and C++ drafts [here](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents). – Shafik Yaghmour Feb 27 '14 at 04:11