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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:24:18+00:00 2026-06-09T22:24:18+00:00

I have one question about the short data type in Java. I know that

  • 0

I have one question about the short data type in Java. I know that the range for short is between -32768 to 32767.

So, if I tried to add two short values that exceed the range, the result ends up being the supposed total minus either the positive range or the negative range times 2, as the following:

short a = 30000;
a = (short) (a+a);

the result is -5536.

So the math is 32768 + 32768 = 65536, 6000 – 65536 = -5536.

I know what it does, but I don’t know why it does it this way.

Can anybody explain the logic or why Java is doing it this way?

  • 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-09T22:24:20+00:00Added an answer on June 9, 2026 at 10:24 pm

    What’s happening is that your number is wrapping around. More specifically, you’ve got a number 30,000, which in binary is:

    0111 0101 0011 0000
    

    When you add it to itself, and carry the 1’s, you get:

    1110 1010 0110 0000
    

    (Note: it’s very easy to multiply a number by 2 in binary — just shift all the bits one to the left.)

    A short is a signed number using two’s complement, meaning that that leftmost 1 is really a minus sign; that number represents -5536.

    If you multiplied that number by 2 again, you’d need more than 2 bytes to represent it. Since you don’t have more than 2 bytes in a short, the extra bits would just get dropped when the int result of the expression is narrowed to a short. Do that enough, and you’ll have a 0 as the leftmost digit; the number is positive again. And then eventually you’ll have a 1 again as the leftmost; the number is negative again. Eventually you’ll have shifted all 0s into the number; multiplying any integer by 2 enough times will always result in 0 (specifically, if it’s an N-bit number, multiplying it by 2 N times will always result in 0).

    If you weren’t narrowing to a short, you’d still eventually run out of digits in the int (when you need 33 or more bits) — this would result in the extra digits being dropped, which is integer overflow. The same thing would happen if either argument were a long, though it would take 65+ bits.

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

Sidebar

Related Questions

i have one question about jquery ajax().. I created a table grid and there
I have one question about searching and counting files in directories and subdirectories. I
I have one question about Yii framework, and I need your help. Basicly, I
Ok, i have one totaly noob question about php sessions: I have 3 (and
I have a question about Criteria method, one-to-many relation to the database, 'one' is
Short Question I have a loop that runs 180,000 times. At the end of
I have one question; In my ASP.NET MVC web application have to do certain
I have one question, I was trying to find more information on the internet
I have one question, in my company we are using Toxicity report to measure
I have one question. I am using Apache CXF framework to generate model classes

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.