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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:49:26+00:00 2026-06-14T06:49:26+00:00

This is a two part question. I have a small code that adds numbers;

  • 0

This is a two part question.

  1. I have a small code that adds numbers; however the numbers often grow faster than long long can hold.

    So I thought I’d make a class to move it over.

    My thought process is given 3 long long a, b, c;,

    Add everything to c and when c is at capacity, add one to b and set c to zeros; once b is “full” add one to a and set b and c to zero and so on.

    So, I have to check to see if c is at capacity before I reset it and add one to b and so on!

    Is there a way to check for that?

  2. Can someone point me to the right direction to make my own data type. Visually, I put 3 long longs together like the above and the data type would do what I talk about above.

Finally, I would like to treat my new data type just like i do to ints:

int a = 0;

I would like to be able to do

mydatatype a = 0;

English is not my first language.

  • 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-14T06:49:27+00:00Added an answer on June 14, 2026 at 6:49 am

    Testing if an integer is at capacity means that your code will be very inefficient. For example, to add 123 you’d need to do 123 increments and 123 comparisons.

    A better way would be to determine if the operation will overflow before performing the operation. For example (for unsigned integers only):

    if(sum <= ULLONG_MAX - a) {
        sum += a;
    } else {
        /* It would have overflowed */
    }
    

    This works because ULLONG_MAX - a can not overflow. When you start looking at signed integers it becomes a larger problem because LLONG_MAX - a can overflow (if a is negative), and LLONG_MIN - a can also overflow (if a is positive). You need to test both ways:

    if( ( a > 0) && (sum <= LLONG_MAX - a) {
        sum += a;
    } else if( ( a < 0) && (sum >= LLONG_MIN - a) {
        sum += a;
    } else if( a != 0) {
        /* It would have overflowed */
    }
    

    Once you’ve determined if it would have overflowed you need to handle that case. For example; if you’re using multiple integers to represent one larger integer; then (for unsigned integers):

    if(sum_low <= ULLONG_MAX - a) {
        sum_low += a;
    } else {
        sum_low -= (ULLONG_MAX - a) + 1;
        sum_high++;
    }
    

    Please note that you have to be very careful to avoid accidental overflows in temporary calculations involved in handling the original (avoided) overflow.

    If you use multiple signed integers to represent one larger signed integer, then the logic behind overflow handling becomes complex and error-prone. It is theoretically possible; however it’s far better to separate the signs of the numbers and then do operation (or its inverse – e.g. subtracting a positive number instead of adding a negative number) on unsigned integers alone; especially if you need to use 3 or more integers to represent one huge integer, or if you need more complex operations like multiplication and division.

    Of course once you start down this path you’re effectively implementing your own “big number” code, and should probably find/use a suitable library instead.

    Finally, if you’d like to treat your new data type like primitive data types (e.g. and be able to do things like mydatatype a = 0;) then you’re out of luck – C doesn’t work this way. Essentially, C is beautiful because it doesn’t allow complex things to be hidden from people trying to read/understand the code; and you’d have to use a less beautiful language like C++ if you want to hide important information from unsuspecting victims. 😉

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

Sidebar

Related Questions

This is a two part question. I have created a user agreement that the
This is a two part question. I have a class that gets all processes
this is a two part question that probbably have a similar answer. I want
this is a two part question. First, I have code to make a call
This is two part question: I have two stored procedures: sp1 & sp2. If
This is kinda....a two part question. The first one is much more important than
This question has two parts. Part 1. Yesterday I had some code which would
This is a two part question in hopes that I can understand more about
This is a two part question, but first some background information: I have a
This is a two part question. Is it possible to take advantage of xVal

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.