Help with cast in C!

This is a discussion on Help with cast in C! within the C Programming forums, part of the General Programming Boards category; Can somebody help me with the cast in C?! I wanna cast 32bit integer into 8bit char value, and the ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    8

    Help with cast in C!

    Can somebody help me with the cast in C?!
    I wanna cast 32bit integer into 8bit char value, and the problem is that I don't know which 8bits of the integer would be taken and stored in the 8bit char variable?! The most significant or least significant?!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you use a plain cast, then the lowest 8 bits are being used. This is probably the answer you wanted.

    Slightly more complex situation:
    If you cast a pointer to another pointer type, and then dereference it, then it's the 8 bits with the lowest address you get. Depending on the byte-order of the machine, you get EITHER the lowest or the highest.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Quote Originally Posted by Tirania
    I wanna cast 32bit integer into 8bit char value, and the problem is that I don't know which 8bits of the integer would be taken and stored in the 8bit char variable?! The most significant or least significant?!!
    According to the C Standard, "if the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined."

    Frankly, it would be clearer to use bitwise operations to extract those bits that you want.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    According to the C Standard, "if the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined."

    Frankly, it would be clearer to use bitwise operations to extract those bits that you want.
    Ah, good point - it is undefined, so the compiler is allowed to do whatever it likes with it. However, I have not yet "met" a compiler that doesn't do what I explained above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Quote Originally Posted by matsp
    Ah, good point - it is undefined, so the compiler is allowed to do whatever it likes with it. However, I have not yet "met" a compiler that doesn't do what I explained above.
    I had in mind an iterative bitwise copy of the 8 bits in question, but come to think of it, the implementation defined behaviour can be avoided with just one bitwise and, e.g.,
    Code:
    char c = (char)(i & 0xff); // where i is the source integer
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 02:32 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 09:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 05:51 PM

Tags for this Thread


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