Negativ number

Discussion in 'Programmer's Corner' started by FroceMaster, Mar 24, 2013.

  1. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    402
    4
    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.
    Code ( (Unknown Language)):
    1.  
    2. offsetgrad--;    
    3.   lcd_goto(0x0e);
    4.   utoa(offgrad, offsetgrad, 10);
    5.   lcd_puts(offgrad);
    6.  
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    what is the data type of offsetgrad?
     
  3. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    402
    4
    signed int offsetgrad;
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    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.
     
  6. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    402
    4

    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.
     
Loading...