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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:48:58+00:00 2026-06-01T03:48:58+00:00

I am trying to solve the problem 2’s complement here (sorry, it requires login,

  • 0

I am trying to solve the problem 2’s complement here (sorry, it requires login, but anyone can login with FB/google account). The problem in short is to count the number of ones appearing in the 2’s complement representation of all numbers in a given range [A, B] where A and B are within the 32-bit limits (231 in absolute value). I know my algorithm is correct (it’s logarithmic in the bigger absolute value, since I already solved the problem in another language).

I am testing the code below on my machine and it’s giving perfectly correct results. When it runs on the Amazon server, it gives a few wrong answers (obviously overflows) and also some stack overflows. This is not a bug in the logic here, because I test the same code on my machine on the same test inputs and get different results. For example, for the range [-1548535525, 662630637] I get 35782216444 on my machine, while according to the tests, my result is some negative overflow value.

The only problem I can think of, is that perhaps I am not using Int64 correctly, or I have a wrong assumption about it’s operation.

Any help is appreciated. Code is here.

  • 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-01T03:49:00+00:00Added an answer on June 1, 2026 at 3:49 am

    The stack overflows are a bug in the logic.

    countOnes !a !b | a == b = countOnes' a
    
    countOnes' :: Int64 -> Integer
    countOnes' !0 = 0
    countOnes' !a = (fromIntegral (a .&. 1)) + (countOnes' (a `shiftR` 1))
    

    Whenever you call countOnes' with a negative argument, you get a nonterminating computation, since the shiftR is an arithmetic shift and not a logical one, so you always shift in a 1-bit and never reach 0.
    But even with a logical shift, for negative arguments, you’d get a result 32 too large, since the top 32 bits are all 1.

    Solution: mask out the uninteresting bits before calling countOnes',

    countOnes !a !b | a == b = countOnes' (a .&. 0xFFFFFFFF)
    

    There are some superfluous guards in countOnes,

    countOnes :: Int64 -> Int64 -> Integer
    countOnes !a !b | a > b = 0
    -- From here on we know a <= b
    countOnes !a !b | a == b = countOnes' (a .&. 0xFFFFFFFF)
    -- From here on, we know a < b
    countOnes !0 !n = range + leading  + (countOnes 0 (n - (1 `shiftL` m)))
        where
            range = fromIntegral $ m * (1 `shiftL` (m - 1))
            leading = fromIntegral $ (n - (1 `shiftL` m) + 1)
            m = (getLog n) - 1
    -- From here on, we know a /= 0
    countOnes !a !b | a > 0 = (countOnes 0 b) - (countOnes 0 (a - 1))
    -- From here on, we know a < 0,
    -- the guard in the next and the last equation are superfluous
    countOnes !a !0 | a < 0 = countOnes (maxInt + a + 1) maxInt
    countOnes !a !b | b < 0 = (countOnes a 0) - (countOnes (b + 1) 0)
    countOnes !a !b | a < 0 = (countOnes a 0) + (countOnes 0 b)
    

    The integer overflows on the server are caused by

    getLog :: Int64 -> Int
    --
    countOnes !0 !n = range + leading  + (countOnes 0 (n - (1 `shiftL` m)))
        where
        range = fromIntegral $ m * (1 `shiftL` (m - 1))
        leading = fromIntegral $ (n - (1 `shiftL` m) + 1)
        m = (getLog n) - 1
    

    because the server has a 32-bit GHC, while you have a 64-bit one. The shift distance/bit width m is an Int (and because it’s used as the shift distance, it has to be).

    Therefore

    m * (1 `shiftL` (m-1))
    

    is an Int too. For m >= 28, that overflows a 32-bit Int.

    Solution: remove a $

    range = fromIntegral m * (1 `shiftL` (m - 1))
    

    Then the 1 that is shifted is an Integer, hence no overflow.

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

Sidebar

Related Questions

I'm trying to solve the problem here but I don't know why my code
I'm trying to solve this problem but it always fails the tests. here's my
I've been trying to solve this problem for a number of days now but
I am trying to solve problem 41 , but i don't know why my
I was trying to solve this problem in UVa, but I get a NullPointerException
I am trying to solve a problem in Objective-C, but I don't think the
I am trying to solve this problem in Haskell but getting time limit exceed.
I'm trying to solve a problem (in php, but programming language doesn't matter). I'm
I've been trying to solve these problem for last two hours but seems like
I have been trying to solve this problem for hours (searched here as well

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.