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

  • Home
  • SEARCH
  • 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 8439023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:53:25+00:00 2026-06-10T07:53:25+00:00

I’ve got a strange problem here, and i’m sure it’s just something small. I

  • 0

I’ve got a strange problem here, and i’m sure it’s just something small.

I recieve information about files via JSON (RestKit is doing a good job).
I write the filesize of each file via coredata to a local store.

Afterwards within one of my viewcontrollers i need to sum up the files-sizes of all files in database. I fetch all files and then going through a slope (for) to sum the size up.

The problem is now, the result is always negative!

The coredata entity filesize is of type Integer 32 (filesize is reported in bytes by JSON).
I read the fetchresult in an NSArray allPublicationsToLoad and then try to sum up. The Objects in the NSArray of Type CDPublication have a value filesize of Type NSNumber:

for(int n = 0; n < [allPublicationsToLoad count]; n = n + 1)
{
    CDPublication* thePub = [allPublicationsToLoad objectAtIndex:n];
    allPublicationsSize = allPublicationsSize + [[thePub filesize] integerValue];
    sum = [NSNumber numberWithFloat:([sum floatValue] + [[thePub filesize] floatValue])];

Each single filesize of the single CDPublications objects are positive and correct. Only the sum of all the filesizes ist negative afterwards. There are around 240 objects right now with filesize-values between 4000 and 234.645.434.123.

Can somebody please give me a hit into the right direction !?
Is it the problem that Integer 32 or NSNumber can’t hold such a huge range?

Thanks

MadMaxApp
}

  • 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-10T07:53:26+00:00Added an answer on June 10, 2026 at 7:53 am

    The NSNumber object can’t hold such a huge number. Because of the way negative numbers are stored the result is negative.

    Negative numbers are stored using two’s complement, this is done to make addition of positive and negative numbers easier. The range of numbers NSNumber can hold is split in two, the highest half (the int values for which the highest order bit is equal to 1) is considered to be negative, the lowest half (where the highest order bit is equal to 0) are the normal positive numbers. Now, if you add sufficiently large numbers, the result will be in the highest half and thus be interpreted as a negative number. Here’s an illustration for the 4-bit integer situation (32 works exactly the same but there would be a lot more 0 and 1 to type;))

    With 4 bits you can represent this range of signed integers:

    0000 (=0)
    0001 (=1)
    0010 (=2)
    ... 
    0111 (=7)
    
    1000 (=-8)
    1001 (=-7)
    ...
    1111 (=-1)
    

    The maximum positive integer you can represent is 7 in this case. If you would add 5 and 4 for example you would get:

    0101 + 0100 = 1001
    

    1001 equals -7 when you represent signed integers like this (and not 9, as you would expect). That’s the effect you are observing, but on a much larger scale (32 bits)

    Your only option to get correct results in this case is to increase the number of bits used to represent your integers so the result won’t be in the negative number range of bit combinations. So if 32 bits is not enough (like in your case), you can use a long (64 bits).

    [myNumber longLongValue];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
i got an object with contents of html markup in it, for example: string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.