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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:39:47+00:00 2026-06-15T18:39:47+00:00

I understand the >>> fixes the overflow: when adding two big positive longs you

  • 0

I understand the >>> fixes the overflow: when adding two big positive longs you may end up with a negative number. Can someone explain how this bitwise shift magically fixes the overflow problem? And how it is different from >> ?


My suspicion: I think it has to do with the fact that Java uses two’s-complement so the overflow is the right number if we had the extra space but because we don’t it becomes negative. So when you shift and pad with zero it magically gets fixed due to the two’s-complement. But I can be wrong and someone with a bitwise brain has to confirm. 🙂

  • 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-15T18:39:47+00:00Added an answer on June 15, 2026 at 6:39 pm

    In short, (high + low) >>> 1 is a trick that uses the unused sign-bit to perform a correct average of non-negative numbers.


    Under the assumption that high and low are both non-negative, we know for sure that the upper-most bit (the sign-bit) is zero.

    So both high and low are in fact 31-bit integers.

    high = 0100 0000 0000 0000 0000 0000 0000 0000 = 1073741824
    low  = 0100 0000 0000 0000 0000 0000 0000 0000 = 1073741824
    

    When you add them together they may “spill” over into the top-bit.

    high + low =       1000 0000 0000 0000 0000 0000 0000 0000
               =  2147483648 as unsigned 32-bit integer
               = -2147483648 as signed   32-bit integer
    
    (high + low) / 2   = 1100 0000 0000 0000 0000 0000 0000 0000 = -1073741824
    (high + low) >>> 1 = 0100 0000 0000 0000 0000 0000 0000 0000 = 1073741824
    
    • As a signed 32-bit integer, it is overflow and flips negative. Therefore (high + low) / 2 is wrong because high + low could be negative.

    • As unsigned 32-bit integers, the sum is correct. All that’s needed is to divide it by 2.

    Of course Java doesn’t support unsigned integers, so the best thing we have to divide by 2 (as an unsigned integer) is the logical right-shift >>>.

    In languages with unsigned integers (such as C and C++), it gets trickier since your input can be full 32-bit integers. One solution is: low + ((high - low) / 2)


    Finally to enumerate the differences between >>>, >>, and /:

    • >>> is logical right-shift. It fills the upper bits with zero.
    • >> is arithmetic right-shift. It fills the upper its with copies of the original top bit.
    • / is division.

    Mathematically:

    • x >>> 1 treats x as an unsigned integer and divides it by two. It rounds down.
    • x >> 1 treats x as a signed integer and divides it by two. It rounds towards negative infinity.
    • x / 2 treats x as a signed integer and divides it by two. It rounds towards zero.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone help me understand why Win32 error codes don't match method signatures and
I’m trying to understand code from http://www.yesodweb.com/book/conduits . After some fixes (like replacing Resource
I can't understand why my server isn't accepting files larger than 1MB. I am
I have been reviewing a number of applications for securing deleting files. I understand
We are planning on adding multi-language support for our ASP.NET Website. I understand the
I understand I can go to http://twitter.github.com/bootstrap/customize.html#components and select the only the components I
I'm learning Squeryl and trying to understand the 'using' syntax but can't find documentation
I have seen a number of old posts around this topic on stack overflow
I am looking for some example which can help me to understand how can
As I understand it, an actor can be sent a message fire and forget

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.