Re: Help me understand this compiler warning.
- From: pete <pfiland@xxxxxxxxxxxxxx>
- Date: Sun, 13 Aug 2006 11:48:24 GMT
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
.
- Follow-Ups:
- Re: Help me understand this compiler warning.
- From: Morris Dovey
- Re: Help me understand this compiler warning.
- References:
- Help me understand this compiler warning.
- From: Charles Sullivan
- Re: Help me understand this compiler warning.
- From: Morris Dovey
- Re: Help me understand this compiler warning.
- From: pete
- Re: Help me understand this compiler warning.
- From: Morris Dovey
- Help me understand this compiler warning.
- Prev by Date: Re: Newbie - itoa implementation
- Next by Date: Re: Newbie - itoa implementation
- Previous by thread: Re: Help me understand this compiler warning.
- Next by thread: Re: Help me understand this compiler warning.
- Index(es):
Relevant Pages
|