c++ - Integer |= Char; operation ignoring high order byte in Integer -


Just a quick and specific question, it stumped me for about half an hour.

  four * bytes = {0x01, 0xD8}; Int value = 0; Value = bytes [0]; // result1 (0x0001) is the value   

The last operation is interested in me, how is it changing the integer signed in -40 in 256?

Edit : For example, a large part of the example code has changed

In your case is equivalent to char signed four, which means that when you value 0xD8 to Char , then this is a negative number <.

| Common arithmetic conversions that occur during the operation are cost-protection, so the negative number is preserved.

To solve the problem, you can either create all your data types as unsigned when you have binary arithmetic or you can call value | = ((Unsigned char) buffer [0]) you can type or value | = Buffer [0] & amp; 0xFF .

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -