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 7070939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:37:37+00:00 2026-05-28T05:37:37+00:00

Possible Duplicate: Integer summing blues, short += short problem I feel dumb that this

  • 0

Possible Duplicate:
Integer summing blues, short += short problem

I feel dumb that this is happening, but I have never had this happen before. I am trying to do the following:

foreach (short a in answers)
{
     if (a != myConstants.NOTCOMPLETE_SHORT)
     {
         result = result + a;
     }
     else
     {
         empty = true;
         break;
     }
}

answers is an array of shorts. intellisense is telling me that result + a is an int and I can’t assign it to a short.

I have to be missing something very fundamental here but not allowing two shorts to be added together and assigned to a short variable just seems weird.

  • 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-05-28T05:37:38+00:00Added an answer on May 28, 2026 at 5:37 am

    Since a short is a 16bit Integer, if you added, say, 32,000 and 32,000 (both valid shorts), you’d get 64,000 which is not a valid short as Int16.MaxValue is 32767.

    Thus, the addition operator must return a 32bit Int to prevent the result from possibly overflowing.

    UPDATE:

    For fun, I just tried this in PowerShell:

    PS C:\> ([System.Int32]::MaxValue + [System.Int32]::MaxValue).GetType().Name
    Double
    
    PS C:\> (10000000 + 10000000).GetType().Name
    Int32
    

    So looks like an Int32 can cast to a Double if needed.

    I’m gonna go out on a limb and say bounds checking during addition is more expensive, and thus should only be done if there’s a likely chance of an overflow (like two bytes or two shorts) especially when they’re possibly using the same amount of memory anyway. I think that’s just the way the language was designed.

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

Sidebar

Related Questions

Possible Duplicate: Integer summing blues, short += short problem I have summarized my problem
Possible Duplicate: php validate integer Currently I'm using this method: Validate the input using
Possible Duplicate: Best way to detect integer overflow in C/C++ i have tried to
Possible Duplicate: PHP Integer Problem 1.0000241703257e+14 how to convert it in PHP, it was
Possible Duplicate: Java Ordered Map I have list of product object in the HashMap<Integer,Product>
Possible Duplicate: New Integer vs valueOf I read at few place that Integer.valueOf is
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
Possible Duplicate: Best way to detect integer overflow in C/C++ If I have an
They think it is: Possible Duplicate: Integer with leading zeroes But if you check
Possible Duplicate: random a 512-bit integer N that is not a multiple of 2,

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.