helloea: Why is u_ch not 127?It should be 255-128=127 when
printing, right?
No, because it has nothing to do with subtraction.
255 (decimal) as unsigned is FF (hex) or 1111 1111 (binary) |
|
As signed, the high bit means negative. The remaining bits mean 7F (hex) or 0111 1111 (binary) or 127 decimal. Thus it is minus 127 |
|
helloea:
However, when printing, ch= -128 u_ch= 128.
Why is u_ch not 127?It should be 255-128=127 when printing, right?
It is related to binary representation of char and uchar.
-128(char) is 1000 0000(BIN), the equivalent is 128(uchar) with the same bits: 1000 0000(BIN).
In this situation a typecasting is not performed, but the memory block is copied.
Typecasting is performed like the following scheme:
According to documentation:
Conversions between char and uchar, short and ushort, int and uint, long and ulong (conversions to both sides), may lead to the loss of data.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
When executing the program, the first time u_ch=ch, ch=-128,u_ch=0.
However, when printing, ch= -128 u_ch= 128.
Why is u_ch not 127?It should be 255-128=127 when printing, right?