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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:30:54+00:00 2026-06-11T16:30:54+00:00

This question is not about how a long should be correctly cast to an

  • 0

This question is not about how a long should be correctly cast to an int, but rather what happens when we incorrectly cast it to an int.

So consider this code –

   @Test
    public void longTest()
    {
        long longNumber = Long.MAX_VALUE;
        int intNumber = (int)longNumber; // potentially unsafe cast.
        System.out.println("longNumber = "+longNumber);
        System.out.println("intNumber = "+intNumber);
    }

This gives the output –

longNumber = 9223372036854775807
intNumber = -1

Now suppose I make the following change-

long longNumber = Long.MAX_VALUE - 50;

I then get the output –

longNumber = 9223372036854775757
intNumber = -51

The question is, how is the long’s value being converted to an int?

  • 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-11T16:30:56+00:00Added an answer on June 11, 2026 at 4:30 pm

    The low 32 bits of the long are taken and put into the int.

    Here’s the math, though:

    1. Treat negative long values as 2^64 plus that value. So -1 is treated as 2^64 – 1. (This is the unsigned 64-bit value, and it’s how the value is actually represented in binary.)
    2. Take the result and mod by 2^32. (This is the unsigned 32-bit value.)
    3. If the result is >= 2^31, subtract 2^32. (This is the signed 32-bit value, the Java int.)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is not technical, but is still about programming. What is the funniest,
My question is not about how to solve this error(I already solved it) but
This question is not about caching. I need this in-memory table for many small
This question is not about which is the best, it is about which makes
This question is not about 'best' barcode library recommendation, we use various products on
(note that this question is not about CAS, it's about the May fail spuriously
For starters, this question is not so much about programming in the NetBeans IDE
This question calls for "line numbers". If you do not care about line numbers
I've this question about pass some instances by ref or not: here is my
Not much to say about this question...

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.