Thread: Value bigger than the signed value

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    3

    Value bigger than the signed value

    The program is a bigger program but the summary of program is

    Code:
    int main(void)
    {
    signed int data1;
    data1 = 32785;
    return 0;
    }
    I know that data1 is signed value and hence the maximum value is 32767. When i store 32785 will the value of data1 be 32767-32785 = -18? But the correct value is -32751. How was it arrived? Please help.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    237
    Lots of things going on here, but in summary, if 'int' is 16 bits, and you have a overflow the answer will be wrong by 2^16, or 65536.

    So what is 32785-65536? −32,751

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    3
    Thank you for the reply. The complete program is

    Code:
    uint32_t startupRamp=0;
    int16_t theta=0;
    void CalculateTheta(void)
    {
         /* Then ramp up till the end */
        if (startupRamp < END_SPEED)
        {
            startupRamp += 10;
        }
        theta += (int16_t)(startupRamp >> 10);
    }
    So my understanding and sequence of steps are
    a. When startupRamp is 32785, its hex value is 0x8011.
    b. The MSBit is 1 hence it is negative value.
    c. Compiler will store the 2's complement number is 32751.
    d. The computer will add a negative sign (since the MSBit is 1) and assign the value of -32751. Is it correct? Please help.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    237
    Quote Originally Posted by CTest123 View Post
    Thank you for the reply. The complete program is

    Code:
    uint32_t startupRamp=0;
    int16_t theta=0;
    void CalculateTheta(void)
    {
         /* Then ramp up till the end */
        if (startupRamp < END_SPEED)
        {
            startupRamp += 10;
        }
        theta += (int16_t)(startupRamp >> 10);
    }
    So my understanding and sequence of steps are
    a. When startupRamp is 32785, its hex value is 0x8011.
    b. The MSBit is 1 hence it is negative value.
    c. Compiler will store the 2's complement number is 32751.
    d. The computer will add a negative sign (since the MSBit is 1) and assign the value of -32751. Is it correct? Please help.
    It's way easier than that. For 16-bit signed numbers the most significant bit has a 'place value' of -32768, rather than the 32768 it would have in an unsigned (or longer) value.

    So 0x8011 as a 16-bit unsigned decimal = 32768+1*16+1 = 32785
    and 0x8011 as a 16-bit signed decimal = -32768+1*16+1 = -32751

  5. #5
    Registered User
    Join Date
    May 2021
    Posts
    3
    Thank you that is really helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why does my array get bigger?
    By slenkar in forum C Programming
    Replies: 6
    Last Post: 11-08-2010, 03:14 PM
  2. small -> big -> bigger -> bigger than bigger -> ?
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-11-2009, 12:12 PM
  3. I need a bigger geek than me
    By ober in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-01-2005, 01:32 PM
  4. Declaring bigger int
    By gustavosserra in forum C Programming
    Replies: 2
    Last Post: 09-20-2003, 04:39 PM
  5. Bigger Avatars
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-31-2001, 10:01 PM

Tags for this Thread