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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:03:08+00:00 2026-06-09T03:03:08+00:00

How can we check whether an arithmetic operation will exceed the upper bounds of

  • 0

How can we check whether an arithmetic operation will exceed the upper bounds of the data type before actually applying it.

Say upperbound for short in java is 32767 and I am multiplying 328*100, I can’t actually do comparison with Short.MAX_VALUE because after multiplication answer would already be overflown and answer would be -32736 which is certainly smaller than Short.MAX_VALUE

Take another example say I am int value of computing 17^10 (17 to the power 10) in a for loop. How do I know at what stage my answer got overflowed.

This Short and int thing is just an example. Think of this problem in bigger perceptive what exactly can be done for all data types.

I tried googling but found no good link which helps understand the concept.

  • 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-09T03:03:11+00:00Added an answer on June 9, 2026 at 3:03 am

    There is a plan to include such methods in the Math package in Java 8, but I don’t know what the current status is. Some source code is available here. I don’t how tested the implementation is, but that could give you ideas.

    For example, int multiplication is done by using longs:

    public static int multiplyExact(int x, int y) {
        long r = (long)x * (long)y;
        if ((int)r != r) {
            throw new ArithmeticException("long overflow");
        }
        return (int)r;
    }
    

    But long multiplication uses a more complex algorithm:

    public static long multiplyExact(long x, long y) {
        long r = x * y;
        long ax = Math.abs(x);
        long ay = Math.abs(y);
        if (((ax | ay) >>> 31 != 0)) {
            // Some bits greater than 2^31 that might cause overflow
            // Check the result using the divide operator
            // and check for the special case of Long.MIN_VALUE * -1
           if (((y != 0) && (r / y != x)) ||
                (x == Long.MIN_VALUE && y == -1)) {
                throw new ArithmeticException("long overflow");
            }
        }
        return r;
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering, will it be possible we can check whether the page is call
How can you check whether a string is convertible to an int? Let's say
Is there any way I can check whether the user has changed any data
I know that I can check whether the linux timestamp in $time is today
Just wondering is there any way I can check whether the url links to
Does anyone know of a utility that can check whether a HTTP request/response is
How can I check whether a numpy array is empty or not? I used
How can I check whether a particular element is inside an array? I don't
How can I check whether a character is a Unicode character or not with
How can I check whether all JavaScript functions have loaded properly on a page?

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.