This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Unexpected behaviour using unsigned char in comparison
- From: "Falk S." <falk-o-mat at gmx dot de>
- To: gcc-help at gcc dot gnu dot org
- Date: Sun, 09 Jan 2011 19:00:04 +0100
- Subject: Unexpected behaviour using unsigned char in comparison
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