[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MiNT] Highwire edit



Helmut Karlowski wrote:

    unsigned short    nkey;
    unsigned char    ascii_code;

ascii_code = nkey & 0x00FF;

But if the target (ascii_code) is 8 bit wide (char) there is no difference I know of.

You are totally right.
Converting an integer type to a smaller integer type removes the highest bits,
so the "bitwise and" is redundant.

The three following lines are strictly equivalent.
ascii_code = nkey & 0x00FF;
ascii_code = nkey;
ascii_code = (unsigned char)nkey;

In the 2 first forms, there is an implicit conversion from unsigned short to unsigned char,
some compilers may issue a warning because of the possible loss of data (if nkey is >= 256)
So the the third form with an explicit cast is the better: the programmer shows explicitly
what he wants, and no compiler will produce a warning.

I have just tested this with the brand new GCC 4.4.1.
In any case, the result is a single "move.b" instruction,
it couldn't be more simple.

--
Vincent Rivière