Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8678433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:43:11+00:00 2026-06-12T20:43:11+00:00

Possible Duplicate: How do promotion rules work when the signedness on either side of

  • 0

Possible Duplicate:
How do promotion rules work when the signedness on either side of a binary operator differ?

I’m trying to wrap my head around integer promotion and overflow in C++. I’m a bit confused with several points:

a) If I have the following code segment:

int i = -15; 
unsigned j = 10; 
std::cout << i + j;

I get out -5 % UINT_MAX. Is this because the expression i + j is automatically promoted to an unsigned? I was trying to read the standard (4.13):

— The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type.

I’m not sure if I’m reading this incorrectly, but if that is true, why is i + j ending up as unsigned?

b) Adding onto the previous segment, I now have:

int k = j + i;

That is getting evaluated to -5. Shouldn’t the expression j + i be evaluated first, giving 4294967291 on my system, and setting that equal to j? That should be out of bounds, so is this behavior undefined? I’m not sure why I get -5.

c) If I change the segment from a) slightly using short, I have:

short i = -15;
unsigned short j = 10;
std::cout << i + j;

I figured when I did this, I would get the same result as a), just with -5 % USHRT_MAX. However, when I execute this, I get -5. Why does using short give a different value than int?

d) I have always learned that the overflow behavior of a signed integral is undefined. For example: int r = ++INT_MAX would be undefined.

However, if there was an unsigned overflow, the quantity would be defined. For example: unsigned a = ++UINT_MAX, then a would be 0. Is that correct?

However, the standard didn’t seem to say anything about it. Is that true? If so, why is that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:43:13+00:00Added an answer on June 12, 2026 at 8:43 pm

    a) From §5/9:

    Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

    • If either operand is of type long double, the other shall be
      converted to long double.
    • Otherwise, if either operand is double, the other shall be converted to double.
    • Otherwise, if either operand is float, the other shall be converted to float.
    • Otherwise, the integral promotions (4.5) shall be performed on both operands.
    • Then, if either operand is unsigned long the other shall be converted to unsigned long.
    • Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int.
    • Otherwise, if either operand is long, the other shall be converted to long.
    • Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

    [Note: otherwise, the only remaining case is that both operands are int]

    Therefore, since j is unsigned, i is promoted to unsigned and the addition is performed using unsigned int arithmetic.

    b) This is UB. The result of the addition is unsigned int (as per (a)), and thus you overflow the int in the assignment.

    c) From §4.5/1:

    An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned int.

    Therefore, since a 4-byte int can represent any value in a 2-byte short or unsigned short, both are promoted to int (per §5.9’s integral promotions rule), and then added as ints.

    d) From §3.9.1/4:

    Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.

    Therefore, UINT_MAX+1 is legal (not UB) and equal to 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: nth-child for every two table rows I'm trying to work my way
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: What does Asynchronous means in Ajax? jQuery ajax return value Trying to
Possible Duplicate: Variable name as a string in Javascript What I'm trying to do
Possible Duplicate: Learning C by K&R, error when trying to compile programs from book
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.