Thread: The largest negative number in 2's complement

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    The largest negative number in 2's complement

    In this exercise:

    The C Programming Language Exercise 3-4

    It states the following:

    "In a two's complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2 to the power (wordsize - 1)) ."

    A char is one byte (255 bits). The range of an 8 bit variable using a two's complement representation is -128 to 127. Therefore -128 is the largest negative value. The statement in book suggests that the itoa function will not output -128 if we pass -128 as a parameter, because in itoa when we try to convert -128 to positive -128, the inverse of -128 is -128. However, I just ran this code in my computer and it successfully outputted -128. Am I missing something?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define SIZE 10
    
    void reverse(char s[])
    {
        int c, i, j;
    
        for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
            c = s[i];
            s[i] = s[j];
            s[j] = c;
        }
    }
    
    void itoa(int n, char s[])
    {
        int i, sign;
    
        if ((sign = n) < 0)
            n = -n;
    
        i = 0;
    
        do {
            s[i++] = n % 10 + '0';
    
        } while ((n /= 10) > 0);
    
    
        if (sign < 0)
            s[i++] = '-';
    
        s[i] = '\0';
    
        reverse(s);
    }
    
    int main(void)
    {
      char buffer[SIZE];
      itoa(-128, buffer);
      printf("Buffer: %s\n", buffer);
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    itoa takes an int as it's first argument, which is usually 32 bits. So presumably that version itoa will fail on -2,147,483,648 not -128.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    62
    When I called the function with itoa(-2147483648, buffer);, I got a warning " warning: this decimal constant is unsigned only in ISO C90 [enabled by default]".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. negative numbers are biased in two's complement?
    By johnmerlino in forum C Programming
    Replies: 4
    Last Post: 03-03-2014, 12:54 AM
  2. Replies: 3
    Last Post: 01-30-2013, 09:30 AM
  3. finding the one's complement of a binary number?
    By dre in forum C Programming
    Replies: 8
    Last Post: 08-23-2009, 05:59 PM
  4. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  5. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM