Thread: unsigned int rollover?

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    unsigned int rollover?

    If I am decrementing an unsigned int (16 bits) by a value say

    k -=15;

    what happens the next time around if k is sitting at a 4? Seems it would roll over to 65524....Is this correct? Can I keep handling it as an unsigned int?

  2. #2
    Registered User
    Join Date
    Mar 2020
    Posts
    12
    Standard seems to define unsigned overflow to work as if modulo 1 + the largest value possible for that type. Signed overflow however is undefined behavior.

    I believe the value should be 65525 though.

    edit: yes, see below:

    Code:
    #include <stdio.h>
    #include <stdint.h>
    
    int main() {
        uint16_t a=4; a-=15;
        printf("%d\n",a);
        return 0;
    }
    It outputs 65525
    Last edited by cronus; 05-28-2020 at 08:22 PM.

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by ridgerunnersjw View Post
    If I am decrementing an unsigned int (16 bits) by a value say

    k -=15;

    what happens the next time around if k is sitting at a 4? Seems it would roll over to 65524....Is this correct? Can I keep handling it as an unsigned int?
    It's undefined behaviour. Best thing to do is check before the subtraction.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    65525, actually, but yes, wraparound is well-defined with unsigned values.
    Code:
    #include <stdio.h>
    #include <stdint.h>
     
    int main()
    {
        uint16_t n = (uint16_t)(-1); // -1 interpreted as unsigned gives largest value
        printf("%u\n", (unsigned)n); // prints 65535
     
        n = 4;
        n -= 15;
        printf("%u\n", (unsigned)n);      // prints 65525
     
        // The bit pattern can be interpreted as a signed value and gives the
        // correct signed answer due to 2's-complement representation.
        printf("%d\n", (int)(int16_t)n);  // prints -11
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by john.c View Post
    65525, actually, but yes, wraparound is well-defined with unsigned values.
    Ah, right! It's only UB with signed values...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-21-2020, 11:39 AM
  2. How covert unsigned char[4] to unsigned integer?
    By barracuda in forum C Programming
    Replies: 110
    Last Post: 02-23-2015, 04:00 AM
  3. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  4. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM
  5. Rollover links
    By NavyBlue in forum Tech Board
    Replies: 10
    Last Post: 09-03-2003, 07:14 AM

Tags for this Thread