Error C1853:unary minus operator.......CW5.7 MC9s08RE16

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error C1853:unary minus operator.......CW5.7 MC9s08RE16

1,371 Views
stevec
Contributor III

I have come up against this error while porting some drivers from another processor

 

C1853: unary minus operator applied to unsigned type

The line in question is:

 

if(offset < 0)

    bytes_to_new_posn -= (unsigned long)0 - offset;

 

bytes_to_new_posn is an unsigned long

offset is a long

 

How do I get round this?

 

Steve

Labels (1)
Tags (1)
0 Kudos
2 Replies

355 Views
BlackNight
NXP Employee
NXP Employee

You get this kind of warning for things like

unsigned char x, y;

 

x = -y;

 

I tried your code with my compiler (from CW 6.2), and did not had this warning from the compiler. Seems like this has been improved. I tried an older compiler from 2000 and had the same warning.

You may try the option

-Ont=-

which disables the '-' operation optimization.

 

In your case it is the simplest way if you change the

(unsigned long)0 - offset

to simply

0 - offset

 

(BTW: '(unsigned long)0' is the same as 0uL).

 

If you want to cast the minus operation result to unsigned long, then this should be

(unsigned long) (0-offset)

 

BK

0 Kudos

355 Views
Lundin
Senior Contributor IV
Your advice doesn't make sense for a standard ISO C program.

unsigned long offset;
0 - offset

is the very same code as

unsigned long offset;
(unsigned long)0 - offset;

In the first case, the conversion of "0" (which has type int) to unsigned long is done implicitly by the "usual arithmetic conversions". In the second case, the usual arithmetic conversions don't need to be used as both types are identical. In both cases, the minus operation is done on two unsigned long variables.


0 Kudos