Negativ number

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Have tried for a while to figure out.
have a simpel number, ex 1, when add 1 it show 2.
when subtract 1 it show 1 Again, and next 0 , but then it show 32767 instead of -1 why ??
number is declared as signed integer.
Rich (BB code):
offsetgrad--;    
  lcd_goto(0x0e);
  utoa(offgrad, offsetgrad, 10);
  lcd_puts(offgrad);
 

t06afre

Joined May 11, 2009
5,934
utoa function is used for converting unsigned numbers to ASCII. For signed numbers try the itoa function :) Every signed argument passed to the utoa function will come out the other end as a unsigned argument. In the binary system the integer number -1 is 0xFFFF
 

ErnieM

Joined Apr 24, 2011
8,377
The reason is signed integer numbers are represented as a two's compliment number. <= Do stop and read that link.

So when you subtract 1 from zero you get -1, which as a 2's compliment integer is represented by 111 1111 1111 1111.

When you pass that to utoa() the number is taken as an unsigned integer, so it looks like a big positive number.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
utoa function is used for converting unsigned numbers to ASCII. For signed numbers try the itoa function :) Every signed argument passed to the utoa function will come out the other end as a unsigned argument. In the binary system the integer number -1 is 0xFFFF

Takker.
that Works. :)
need to make changes in the temperature Circuit, the voltage drops to much when wire is around 10 m. to the sensor.
 
Top