Re: Help me understand this compiler warning.



Morris Dovey wrote:

pete (in 44DE53F8.F87@xxxxxxxxxxxxxx) said:

| Morris Dovey wrote:
||
|| Charles Sullivan (in pan.2006.08.12.21.25.55.637468@xxxxxxxxxxxx)
|| said:
||
||| I have a program written in C under Linux (gcc) which a user has
||| ported to run under AT&T SysV R4.
|||
||| He sent me a copy of his makelog which displays a large number
||| of compiler warnings similar to this:
|||
||| warning: semantics of ">>" change in ANSI C; use explicit cast
|||
||| The statement to which this applies is:
||| xuc = ((uc[7] & 0xF0 ) >> 4);
|
| I think the compiler wants to see it this way:
|
| xuc = (unsigned char)((uc[7] & 0xF0 ) >> 4);

Since xuc is unsigned. Casting the result isn't necessary in order for
the assignment to be made as intended. ANDing an unsigned char with an
int /could/ produce a signed int - which, according to 6.5.7(5), could
produce an implementation-defined shifted result.

Better to ensure that the quantity to be shifted is unsigned.

||| where the declarations for xuc and uc are:
||| extern unsigned char xuc;
||| unsigned char uc[20];
||
|| Try:
||
|| xuc = ((uc[7] & 0xF0u) >> 4);
||
|| I'd use:
||
|| xuc = (uc[7] >> 4) & 0x0F;
||
|| to ensure that the shifted portion is not promoted to an int.
|
| I don't think that does what you want.
|
| N869
| 6.5.7 Bitwise shift operators
|
| [#3] The integer promotions are performed on each of the
| operands.

You appear to have missed that part of 6.7.5(3) that says the result
will have the type of the promoted left operand - unsigned if done as
I suggest.

6.3.1.8(1) - rules for integer promotions - adds a bit of
clarification (sort of).

The left operand is uc[7], which is of type unsigned char.

Your posted code doesn't do anything to prevent
type unsigned char from being promoted to type int.

Why do you think that in
(uc[7] >> 4)
that the left operand won't be promoted to int?

"If an int can represent all values of the original type, the
value is converted to an int; otherwise, it is converted to
an unsigned int. These are called the integer
promotions."

--
pete
.



Relevant Pages

  • Re: Help me understand this compiler warning.
    ... | The left operand is uc, which is of type unsigned char. ... | type unsigned char from being promoted to type int. ...
    (comp.lang.c)
  • Re: complement of an unsigned char
    ... Isn't an unsigned char 8 bits long? ... only if the corresponding bit in the converted operand is not set). ... So c is promoted to int (unless CHAR_MAX> INT_MAX, ... promotions are done. ...
    (comp.lang.c.moderated)
  • Re: va_args Conformance
    ... static void test{ ... The second argument of his call to testhas a type of int. ... va_argwith a type of one_char, which is a typedef for unsigned char. ... promoted according to the default argument promotions), ...
    (comp.lang.c)
  • Re: va_args Conformance
    ... static void test{ ... unsigned char * byte; ... The second argument of his call to testhas a type of int. ... promoted according to the default argument promotions), ...
    (comp.lang.c)
  • Re: Why is this cast nessasary
    ... I don't know what you mean by "The Compiler is 8 Bits". ... bit com_putchar (unsigned char c) ... any subexpression of type unsigned char is going to ... be promoted to either int or unsigned int. ...
    (comp.lang.c)