Thread: WORD Casting

  1. #1
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42

    WORD Casting

    Hi All,

    Is there any difference?
    Code:
    unsigned char data;
    unsigned int worddata;
    
    data = (unsigned char)worddata;
    and
    Code:
    data = (unsigned char)(0x00FF & worddata);

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    data = (unsigned char)(0x00FF & worddata);
    I'm guessing it's the wrong way around (because that makes no sense). Perhaps it should be:
    Code:
    data = (unsigned char)(worddata & 0x00FF);
    And if that is the case, then yes, they are different.
    Careful here, because
    Code:
    (unsigned char)worddata
    Is the same as
    Code:
    worddata & 0xFF
    While the second example uses a different mask (gets the second byte instead of the first).

    Results may vary on different endian systems, but the two lines of code are clearly different.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    0xFF & x and x & 0xFF are exactly the same thing.

    The truth table for AND is symmetrical, so 1 & 0 or 0 & 1 will both result in 0, 1 & 1 will result in 1, 0 & 0 results in 0.

    A cast to unsigned char, assuming char is 8 bits, should be implemented by the compiler as something equivalent to x & 0xff.

    --
    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.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    0xFF & x and x & 0xFF are exactly the same thing.
    I see, though it looked kind of reversed, and as it seems, applying a mask to 0xFF rather than to x would yield different results, but what do you know...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    Quote Originally Posted by Elysia View Post
    Code:
    data = (unsigned char)(worddata & 0x00FF);
    And if that is the case, then yes, they are different.
    Careful here, because
    Code:
    (unsigned char)worddata
    Is the same as
    Code:
    worddata & 0xFF
    While the second example uses a different mask (gets the second byte instead of the first).

    Results may vary on different endian systems, but the two lines of code are clearly different.
    So what you recommend for good coding practice? Which casting is better (safer, faster etc.)?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nenpa8lo View Post
    So what you recommend for good coding practice? Which casting is better (safer, faster etc.)?
    I'd just use a cast myself. The char can not hold more than 0xff anyways, so some way or another, the compiler will have to do the necessary operations to chop it down. Of course, it may be more obvious what you want to do if you also and with 0xFF - but the risk is that the compiler doesn't understand what's going on, and adds the extra and into the code [it is, after all what you asked for, even if it's pointless].

    --
    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.

  7. #7
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    And another one.
    Code:
    WORD data;
    data = 0xE9E9;
    data >>= 8;
    is it always true that bits on the left hand side are '0' ? And in this case final value is 0x00E9? What if I want 3th byte from long (4B)?
    Code:
    LONG Data = 0x12345678;
    BYTE byte;
    
    byte = (BYTE)(Data >> 16);
    or
    Code:
    byte = (BYTE)((Data & 0x00FF0000) >> 16);
    Which is better (safer, faster etc.)?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The best thing to do is to explicitly specify what data you want to keep. You can do this by shifting.
    For example:
    Code:
    unsigned short data = 0xFF00;
    unsigned char data2 = (char)(data >> 8);
    By shifting the data into the correct place, it is then safe to truncate the data because you've just made sure that the data you want to keep is placed in the first byte.
    Endianness is a tricky beast, so this may or may not work. Something better would probably be:
    Code:
    unsigned int data3 = 0xFFFF0000;
    unsigned short data4 = (data3 >> 16);
    Because by shifting the data into the right "place", it is now merely 16 bits, which can be represented inside a short.

    But the general rule is: don't truncate data unless necessary. Keep the chain, keep the size.
    Last edited by Elysia; 05-22-2008 at 02:57 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nenpa8lo View Post
    And another one.
    Code:
    WORD data;
    data = 0xE9E9;
    data >>= 8;
    is it always true that bits on the left hand side are '0' ? And in this case final value is 0x00E9?
    For an unsigned value, yes. For a signed value, the sign should fill the upper bits (and in this case, that would mean 1's, since the top bit of the original value is set).
    What if I want 3th byte from long (4B)?
    Code:
    LONG Data = 0x12345678;
    BYTE byte;
    
    byte = (BYTE)(Data >> 16);
    or
    Code:
    byte = (BYTE)((Data & 0x00FF0000) >> 16);
    Which is better (safer, faster etc.)?
    Assuming that BYTE is indeed a byte (such as unsigned char on a "normal" machine), there is no need to use and here, a cast is sufficient. It also makes no difference if you use and first, or last (after the shift) from a logical perspective, but an and with 0xFF is potentially shorter than the and with a 32-bit number (0x00FF0000), so anding last [assuming we store the result in, say, a 32-bit integer, so we need the AND to make sure we get rid of un-needed bits].

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM