how gcc thinks `char' as signed char or unsigned char ?

John Love-Jensen eljay@adobe.com
Wed Mar 5 13:08:00 GMT 2008


Hi PRC,

In C++ (and C too), it is really best to think of char as non-signed.

If you need to manipulate bytes of data, take a page from the Java
programming manual and do this:

typedef char byte;
byte b = GetByte();
if ((b & 0xFF) < 200)
{
  std::cout << "byte is under 200" << std::endl;
}

The explicit (b & 0xFF) converts the byte to an int, and ensures that it is
between 0 and 255.

Also, the explicit (b & 0xFF) tells any other programmer that follows in
your footsteps that the value is ensured to be between 0 and 255.

It's safe from char being signed or unsigned, which is not reliable from
platform to platform.

Also, using 'byte' instead of 'char' is a way to convey, in code (rather
than in comment) that you are working with byte information and not
character information.

(The above assumes that the size of a byte is an octet.  If you are on a
platform that has a byte of a different bit-size, you may need to
accommodate accordingly.)

And on my platforms, (b & 0xFF) optimizes very well.  Apparently most CPUs
are pretty good at bit twiddling.  :-)

Some may advocate using 'unsigned char' for byte.  I used to advocate that,
too (via: typedef unsigned char byte;).  After a stint doing Java
development, I've changed my mind and now much prefer using an unspecified
'char' for byte (via: typedef char byte;), and employ the (b & 0xFF)
paradigm when/where needed.  More typing, but -- in my opinion -- much
better code clarity, better self-documenting code, and less obfuscation.

In the end, it's a matter of coding style and personal preference.  The
above is my recommendation, for you consideration.

HTH,
--Eljay



More information about the Gcc-help mailing list