chars promoted to ints?
Steve64
Posts: 11
Do the following...
1.····· char x1 = 1;
2.····· char x2 = 2;
3.····· char x3 = 0xcf;
4.····· char x4;
5.
6.····· x4 = x1;
7.····· x4 += x2;
8.····· x4 += x3;
After step 6, x4 = 1
After step 7, x4 = 3
After step 8, x4 = 0xffd2
Why is·x4 being promoted to an int and sign extended?
Documentation say that char is 8 bit unsigned...
How would I do 8 bit unsigned math?
Thanks
Steve
·
1.····· char x1 = 1;
2.····· char x2 = 2;
3.····· char x3 = 0xcf;
4.····· char x4;
5.
6.····· x4 = x1;
7.····· x4 += x2;
8.····· x4 += x3;
After step 6, x4 = 1
After step 7, x4 = 3
After step 8, x4 = 0xffd2
Why is·x4 being promoted to an int and sign extended?
Documentation say that char is 8 bit unsigned...
How would I do 8 bit unsigned math?
Thanks
Steve
·
Comments
Except arrays, int[noparse]/noparse and short[noparse]/noparse and boolean[noparse]/noparse occupy 2 bytes/element
char[noparse]/noparse and byte[noparse]/noparse occupy 1byte/element
The easiest way to prevent sign extension is to AND any expression result
with 0xFF.
char x = (expression)&0xFF;
regards peter