Complement of unsigned char

Discussion in 'C Programming' started by omisols@gmail.com, Mar 29, 2006.

  1. Guest

    Gurus -

    Can anyone explain the behavior here?

    #include <stdio.h>
    #include <stdlib.h>

    int main ()
    {
    unsigned char c = 0;

    printf (" c = %u, ~c = %u \n", c, ~c);

    return 0;
    }

    The output for the above chunk of code (when run on a linux box with
    gcc) is:
    c = 0, ~c = 4294967295

    The value for ~c is (2 ^ 32) - 1.

    Isn't an unsigned char 8 bits long?
    If that's the case, should the value of ~c be ( 2 ^ 8 ) - 1 == 255?

    I'm confused.

    Thanks,
    OmiSols
    , Mar 29, 2006
    #1
    1. Advertising

  2. Joe Wright Guest

    wrote:
    > Gurus -
    >
    > Can anyone explain the behavior here?
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main ()
    > {
    > unsigned char c = 0;
    >
    > printf (" c = %u, ~c = %u \n", c, ~c);
    >
    > return 0;
    > }
    >
    > The output for the above chunk of code (when run on a linux box with
    > gcc) is:
    > c = 0, ~c = 4294967295
    >
    > The value for ~c is (2 ^ 32) - 1.
    >
    > Isn't an unsigned char 8 bits long?
    > If that's the case, should the value of ~c be ( 2 ^ 8 ) - 1 == 255?
    >
    > I'm confused.
    >
    > Thanks,
    > OmiSols
    >

    You've run afoul of promotions. Look 'em up in your C book. In the
    context of the printf function, the expression c yields an int value.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Mar 29, 2006
    #2
    1. Advertising

  3. Guest

    Thanks Joe!

    I casted it explicitly and it worked as expected.
    , Mar 29, 2006
    #3
  4. SM Ryan Guest

    # The output for the above chunk of code (when run on a linux box with
    # gcc) is:
    # c = 0, ~c = 4294967295
    #
    # The value for ~c is (2 ^ 32) - 1.
    #
    # Isn't an unsigned char 8 bits long?

    Not necessarily.

    # If that's the case, should the value of ~c be ( 2 ^ 8 ) - 1 == 255?

    The (unsigned char) is converted to (unsigned int) when it shows up
    in an expression.


    #include <stdio.h>

    int main(int N,char **P) {
    unsigned char x,y;
    x = 0xE1; y = ~x;
    printf("%u %u %u %u %u %u\n",x,x|0,y,y|0,~x,~x|0);
    return 0;
    }


    @ cc x.c; a.out
    225 225 30 30 4294967070 4294967070

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    So....that would make Bethany part black?
    SM Ryan, Mar 29, 2006
    #4
  5. SM Ryan <> writes:
    [...]
    > The (unsigned char) is converted to (unsigned int) when it shows up
    > in an expression.


    If int can represent all the values of unsigned char (as it can on
    most systems), the unsigned char value is promoted to (signed) int.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 29, 2006
    #5
  6. Pedro Graca Guest

    wrote:
    > I casted it explicitly and it worked as expected.


    What did you cast to have it work as expected?
    What were the expectations? What was the previous error?

    Please quote context in your followups.
    Remember to keep attribution lines (the "somebody wrote:" lines) and
    trim text not relevant to your followup.
    Also read the link in my signature.


    If you have a C99 compiler, you can use the "hh" length modifier for
    the printf() conversion specification:

    #include <stdio.h>
    /* #include <stdlib.h> */

    int main (void)
    {
    unsigned char c = 0;

    printf("unsigned char: c = %hhu, ~c = %hhu\n", c, ~c);
    return 0;
    }


    --
    If you're posting through Google read <http://cfaj.freeshell.org/google>
    Pedro Graca, Mar 29, 2006
    #6
  7. Guest

    > wrote:
    > > I casted it explicitly and it worked as expected.

    >
    > What did you cast to have it work as expected?

    printf (" c = %u, ~c = %u \n", (unsigned char) c, (unsigned char)
    (~c)); worked. And by worked I mean, I wanted to see 255 printed and
    the above statement did that.

    > What were the expectations? What was the previous error?

    I wanted to see 255 printed, however I was seeing 4294967295 printed
    instead.

    >
    > Please quote context in your followups.
    > Remember to keep attribution lines (the "somebody wrote:" lines) and
    > trim text not relevant to your followup.
    > Also read the link in my signature.

    Will do that from now on.

    >
    > If you have a C99 compiler, you can use the "hh" length modifier for
    > the printf() conversion specification:
    >
    > #include <stdio.h>
    > /* #include <stdlib.h> */
    >
    > int main (void)
    > {
    > unsigned char c = 0;
    >
    > printf("unsigned char: c = %hhu, ~c = %hhu\n", c, ~c);
    > return 0;
    > }

    Didn't try this yet. But am sure it'll work.
    , Mar 29, 2006
    #7
  8. writes:
    [...]
    >> Please quote context in your followups.
    >> Remember to keep attribution lines (the "somebody wrote:" lines) and
    >> trim text not relevant to your followup.
    >> Also read the link in my signature.

    > Will do that from now on.


    The article you're replying to was written by Pedro Graca, but I can't
    tell that by reading your followup. Please don't delete attribution
    lines (e.g., the " writes:" line above).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 29, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor

    sign magnitude, ones complement, two's complement

    Mantorok Redgormor, Oct 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    8,598
    Glen Herrmannsfeldt
    Oct 8, 2003
  2. sarathy

    1's complement and 2's complement

    sarathy, Aug 1, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,190
    Bo Persson
    Aug 2, 2006
  3. subramanian

    one's complement of unsigned char

    subramanian, Dec 29, 2006, in forum: C Programming
    Replies:
    6
    Views:
    438
    Peter Nilsson
    Dec 30, 2006
  4. sarathy
    Replies:
    22
    Views:
    2,347
    Bo Persson
    Aug 2, 2006
  5. Roberto Waltman

    2's complement vs. 1's complement vs. ...

    Roberto Waltman, Jun 9, 2011, in forum: C Programming
    Replies:
    4
    Views:
    1,355
    Michael Press
    Jun 14, 2011
Loading...

Share This Page