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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:12:52+00:00 2026-05-12T09:12:52+00:00

I am getting OverflowException ‘s thrown at me when I don’t want them (or

  • 0

I am getting OverflowException‘s thrown at me when I don’t want them (or so I think). I am performing some odd calculations where I expect the values to overflow, discarding overflowed bits. It seems I can’t get this to work properly though. Basically this is one pair of i and j which happens as I iterate over huge sets (int.MinValue to int.MaxValue).

// i and j are ints
// i is -2147483648
// j is -1
var x = i / j;

// I also tried using unchecked keyword, but it doesn't help    
var x = unchecked(i / j);

Update:

The expected mathematic value of -2147483648 / -1 is 2147483648. However, this certain code doesn’t really attempt to find the number. This is part of a series of bit manipulation things gone a bit hard to understand. To be honest, I don’t even know myself what the intention was because I didn’t really document the method, and all it took was one day away from it to raise serious WTF-bubbles over my head. All I know it works as intended with special code designed to handle the case.

About the expected value:

Since int can only hold 2147483647 at it’s max, I expect to discard the overflow yielding value 0.

If I’ve learned anything about this at all, it’s probably the importance of documentation for obscure methods.

  • 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-12T09:12:53+00:00Added an answer on May 12, 2026 at 9:12 am

    I believe this is the only case in which you’ll get this exception. It’s the only division within the Int32 range which can overflow. (There’s division by zero of course, but that’s a different exception.)

    So if you want to avoid OverflowException you only need to deal with this case. What do you want it to do? Write a method which spots this exact case, and otherwise does normal division.

    Note: this is also why you shouldn’t try to reverse a comparison by just negating the result. If you want to sort something in descending order instead of ascending, then instead of using -Compare(x, y) use Compare(y, x). The negation doesn’t give overflow for int.MinValue (unless you’re in a checked context) – it just silently returns int.MinValue.

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

Sidebar

Related Questions

Getting started with TDD and I want to ground up a Repository-driven Model. However,
Getting some zlib error in the middle of cloning big (4 GB) repo from
Getting ready to clean up some old tables which are no longer in use,
Getting through some practice exam papers but in regards to the relational algebra I'm
Getting thrown into ColdFusion dev at work and just starting out, I wonder if
In the Method below on the last line I'm always getting an exception: System.OverflowException:
Getting an error running a Android unit test which extends ActivityInstrumentationTestCase2, and so want
Getting an odd problem with a unit test in my solution. One of the
I am getting some client-side Javascript stack overflow issues specifically in IE browser, this
getting this error while linking an app, some one an idea? ld: duplicate symbol

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.