Thread: Numeric Ranges

  1. #1
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42

    Numeric Ranges

    If int one_billion = 1000000000 is times by three it displays -1294967296.
    Apparently a truncating happens? but I thought truncating would suggest a chopping off of digits?...however 1 billion times three would add any digits? it can already display 10 digits it would make sense that it could display upto..9999999999 right ?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It still is truncated, but remember that a signed integer is represented by two's complement. So for example "0x7FFF" is 32767 for both unsigned and signed short, but "0x8000" is 32768 for unsigned and -32768 for signed.

    And no, a typical int( one that is 32 bits ) can represent anything from 0 to 4294967295.
    Devoted my life to programming...

  3. #3
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    so its a signed int short it that gets overloaded? once it gets over 0x7FFF it gets inverted ? going from 32768 to -32768 "FFFFFFFFFFFF8000".?..

  4. #4
    Guest
    Guest
    It basically behaves like this (using a 3 bit number here):
    Code:
    binary  unsigned  signed
    
       000         0       0
       001         1       1
       010         2       2
       011         3       3
       100         4      -4
       101         5      -3
       110         6      -2
       111         7      -1
    Demo:
    Code:
    void print(unsigned short us) {
        cout << "unsigned(" << us << ") -> signed(" << static_cast<signed short>(us) << ")\n";
    }
    
    int main()
    {
        unsigned short us;
    
        // Lower values identical
        for (us = 0; us < 8; ++us) {
            print(us);
        }
    
        cout << '\n';
    
        // Mid values, signed type becomes negative (starting with maximum negative value)
        for (us = 32764; us < 32772; ++us) {
            print(us);
        }
    
        cout << '\n';
    
        // Highest values, signed type has counted down to -1
        for (us = 65528; us < 65535; ++us) {
            print(us + 1);
        }
    
        cout << '\n';
    }
    Last edited by Guest; 12-03-2017 at 10:46 AM. Reason: typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with <algorithm> functions with 2 ranges
    By nimitzhunter in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2014, 03:20 PM
  2. alpha-numeric to numeric program
    By johnhuge in forum C Programming
    Replies: 2
    Last Post: 03-24-2012, 12:08 AM
  3. declaring ranges a-z A-Z 0-9 etc. ?
    By kezkez in forum C Programming
    Replies: 1
    Last Post: 07-01-2010, 05:48 PM
  4. Asserts and their ranges
    By Matamoros123 in forum C++ Programming
    Replies: 8
    Last Post: 10-07-2006, 12:32 PM
  5. C Ranges
    By EvoTone in forum C Programming
    Replies: 20
    Last Post: 09-28-2006, 05:40 AM

Tags for this Thread