question about casting

This is a discussion on question about casting within the C Programming forums, part of the General Programming Boards category; Code: char c = 0xff; unsigned int a; a = (unsigned int) c; How come in the above cast "a" ...

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    question about casting

    Code:
    char c = 0xff;
    unsigned int a;
    
    a = (unsigned int) c;
    How come in the above cast "a" becomes ULONG_MAX or 0xffffffff? Since I am
    casting to unsigned, shouldn't it cast to 0x000000ff, or would I have to declare
    c as unsigned char in order for that to happen?

    thanks
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How come in the above cast "a" becomes ULONG_MAX or 0xffffffff?
    Sign extension. Your char type is signed by default, so assigning it 0xff makes it negative. Casting to unsigned int then sign extends the value to 0xffffffff.

    >or would I have to declare c as unsigned char in order for that to happen?
    Try it and see.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    58
    don't use unsigned, it just creates trobules.

    in some cases
    Code:
    unsigned int a = 5;
    int b = 5;
    a==b is not true.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,637
    That's just weird advice that has nothing to do with the question, and moreover, it is indeed safe compare signed stuff with signed stuff, and compare unsigned stuff with unsigned stuff without changing the logical answer: using the same signedness on both sides of a comparison makes the bit pattern deterministic.
    Last edited by whiteflags; 07-09-2007 at 08:27 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Govalant View Post
    a==b is not true.
    I'd like to know more about that, or a link or something.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,637
    Quote Originally Posted by robwhit View Post
    I'd like to know more about that, or a link or something.
    http://en.wikipedia.org/wiki/Signed_...epresentations

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Govalant View Post
    don't use unsigned, it just creates trobules.

    in some cases
    Code:
    unsigned int a = 5;
    int b = 5;
    a==b is not true.
    Your compiler is broken. It's time to get a new one.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by swoopy View Post
    Your compiler is broken. It's time to get a new one.
    Yep. What he says is in direct contradiction to ISO/IEC 9899 section 6.3 (2) and section 6.3.1.3 (1).

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by brewbuck View Post
    Yep. What he says is in direct contradiction to ISO/IEC 9899 section 6.3 (2) and section 6.3.1.3 (1).
    I think I'll read through that section tomorrow just for fun.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question on casting 0
    By laertius in forum C Programming
    Replies: 2
    Last Post: 11-09-2008, 11:15 AM
  3. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21