This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Unexpected behaviour using unsigned char in comparison


On 1/9/2011 10:00 AM, Falk S. wrote:
Hi,

in the following program the return value of the test2uc() function is always false, however I would expect it to be true.
#include <iostream>
using namespace std;


bool test2uc() {
       typedef unsigned char uchar;
       uchar mask = ~(uchar(0)) << 1;
       return (mask | uchar(true)) == (~ uchar(0));
}

int main(int argc, char** args) {
       cout << test2uc() << endl;
       return 0;
}

Compiled with -ansi -Wall -g -gdwarf-2 -pedantic
g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
g++.exe (GCC) 4.5.0 (MingW)
on Windows 7

I get a "warning: comparison is always false due to limited range of data type" when compiling with g++ a c++ file, which I do not understand.
If I change the type of uchar to char, i.e. signed char, I get the expected result.


Any ideas?
bye,
Falk
Not sure what you're trying to do, but I find it a bit odd that you're doing bit operations until you do (mask | uchar(true)).

Did you intend to use "||"?

--Bob



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]