Shop OBEX P1 Docs P2 Docs Learn Events
chars promoted to ints? — Parallax Forums

chars promoted to ints?

Steve64Steve64 Posts: 11
edited 2006-10-02 20:25 in General Discussion
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


·

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-02 20:25
    All scalar types (char,byte,int,short,boolean) occupy 2 bytes.
    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
Sign In or Register to comment.