I've had a good look around and can't find a similar question so apologies if it has been asked before.
I'm just playing around with types and numbers and I am wondering if the following behaviour can be guaranteed.
If I declare 2 variables as
unsigned char BIT_8 = 0;
unsigned short int BIT_16 = 0xFF01;
and then do the following (ignoring C style cast for now, unless that can affect it?)
cout << "BIT_16: " << BIT_16 << "
";
cout << "BIT_8: " << (int)BIT_8 << "
";
BIT_8 = BIT_16;
cout << "BIT_8 after: " << (int)BIT_8 << "
";
BIT_8 = BIT_16 >> 8;
cout << "BIT_8 after shift: " << (int)BIT_8 << "
";
I get the output
BIT_16: 65281
BIT_8: 0
BIT_8 after: 1
BIT_8 after shift: 255
Is it guaranteed that if I cast a 16 bit type to an 8 bit type that the leading byte will be lost? or is it undefined and the above results are luck?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…