comparisons errors

Discussion in 'C++' started by hyling.s1@gmail.com, Nov 27, 2007.

  1. Guest

    Would someone please enlighten me as to why each version behaves the
    way it does?

    char temp[12];

    int temp2 = sizeof(char);
    QCoreApplication a(argc, argv);

    temp[0] = 0xFF;
    temp[1] = 0xEE;

    if(temp[0] == 0xFF) // returns false but if(temp[0] == (char)0xFF) //
    returns true

    {
    printf("yes");
    }
    else
    {
    printf("no");
    }

    Thanks
    Hua-Ying
     
    , Nov 27, 2007
    #1
    1. Advertising

  2. The range of char is simply just [-128, 127] so using
    > if(temp[0] == 0xFF)

    returns always false as 0xFF = 255 is not in the range of char.

    My version of gcc gives the warning "[Warning] comparison is always
    false due to limited range of data type".
     
    Eero Harmaala, Nov 27, 2007
    #2
    1. Advertising

  3. Mike Wahler Guest

    <> wrote in message
    news:...
    > Would someone please enlighten me as to why each version behaves the
    > way it does?
    >
    > char temp[12];
    >
    > int temp2 = sizeof(char);
    > QCoreApplication a(argc, argv);
    >
    > temp[0] = 0xFF;
    > temp[1] = 0xEE;
    >
    > if(temp[0] == 0xFF) // returns false but if(temp[0] == (char)0xFF) //
    > returns true
    >
    > {
    > printf("yes");
    > }
    > else
    > {
    > printf("no");
    > }
    >
    > Thanks
    > Hua-Ying


    If type 'char' on your implementation is signed, it isn't
    guaranteed to be able to represent 0xFF. If your CHAR_BIT
    is 8, and type 'char' is signed, then 0xFF will probably be
    interpreted as -1, not 255.

    With MSVC++, whose size 'char' is signed and has eight bits:

    #include <iostream>

    int main()
    {
    int i = 0xFF;
    char c = 0xFF;
    std::cout << "i == " << i << '\n'
    << "c == " << int(c) << '\n';

    return 0;
    }

    Output:

    i == 255
    c == -1

    Try using unsigned char instead of char.

    -Mike
     
    Mike Wahler, Nov 27, 2007
    #3
  4. wrote:
    > ...
    > temp[0] = 0xFF;
    > temp[1] = 0xEE;
    >
    > if(temp[0] == 0xFF) // returns false but if(temp[0] == (char)0xFF) //
    > returns true
    > ...


    Let's get rid of the variables and convert everything to decimals. The
    above comparison is equivalent to

    if ((char) 255 == 255)

    Before the comparison takes place, the language will apply arithmetic
    promotions to both operands and promote them to type 'int' (assuming
    that in your compiler the range of 'char' fits into the range of 'int').
    The actual comparison will be carried out for 'int' operands. This means
    that what you wrote above is equivalent to

    if ((int)(char) 255 == (int) 255)

    Now, let's assume that in your compiler the type 'char' is signed and
    it's range is too small to accommodate the value 255. In that case the
    result of '(char) 255 ' conversion is implementation-defined. Most
    likely it is going to be some negative value, say, '-1'. Under that
    assumption, the above comparison is equivalent to

    if (-1 == 255)

    and, obviously, the result of this comparison is 'false'.

    Once you add the extra 'char' cast on the right-hand side, you end up with

    if ((int)(char) 255 == (int)(char) 255)

    which is going to be equivalent to

    if (-1 == -1)

    for the very same reasons (under the very same assumptions). This is, of
    course, 'true'.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 27, 2007
    #4
  5. Hua-Ying Guest

    Thanks you for the replies everyone!! It was certainly enlightening!

    Hua-Ying
     
    Hua-Ying, Nov 27, 2007
    #5
  6. Jack Klein Guest

    On Tue, 27 Nov 2007 21:03:53 +0200, Eero Harmaala
    <> wrote in comp.lang.c++:

    > The range of char is simply just [-128, 127] so using
    > > if(temp[0] == 0xFF)

    > returns always false as 0xFF = 255 is not in the range of char.


    You mean the range of char ***in your particular implementation***.
    The plain char type may be either signed or unsigned. It must be able
    to contain at least a range of either -127 (not -128) to 127, or 0 to
    255. It may contain a far wider range.

    > My version of gcc gives the warning "[Warning] comparison is always
    > false due to limited range of data type".


    Try a typical compiler for an ARM processor, just for example, or a
    DSP with 16 or 32 bit characters, and this warning won't appear.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Nov 28, 2007
    #6
    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. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    991
    Mark Goldin
    Jan 17, 2004
  2. Chad Z. Hower aka Kudzu

    ASP.net comparisons (ANN?)

    Chad Z. Hower aka Kudzu, Feb 16, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    372
    Chad Z. Hower aka Kudzu
    Feb 17, 2004
  3. VB Programmer

    Question: Time comparisons

    VB Programmer, Jul 14, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    319
    Lucas Tam
    Jul 14, 2004
  4. Digital Puer

    market comparisons of JSP vs. others?

    Digital Puer, Jul 25, 2003, in forum: Java
    Replies:
    7
    Views:
    1,614
    Bruce Lewis
    Jul 28, 2003
  5. GaryM

    Day boundary comparisons

    GaryM, Apr 17, 2004, in forum: Java
    Replies:
    2
    Views:
    354
    GaryM
    Apr 18, 2004
Loading...

Share This Page