• Advertisement
Sign in to follow this  

Beyond 4294967295

This topic is 1806 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to make this number larger using the "int" besides "long int"

 

So lets say I want an unlimited number (which ill never use). More curious then anything.
 

 

#include <iostream>
using namespace std;
 
int main()
{
    //integer wrap around
    score = 4294967295;
    cout << "score: " << score << endl;
 
    ++score;
    cout << "score: " << score << endl;
 
    return 0;
}
 
So instead of making this number wrap around keep going up and up?

Share this post


Link to post
Share on other sites
Advertisement

Or you can easily implement your own big int library. It is really easy task. Last year, when I was 9th grade one of the problem we needed to solve in our local programming competition was:

Without use of any other libraries(ohh they gave us one of the first borland compilers... C++ without namespaces. I was shocked then) we had to find the 1000th fibonacci and check if it can be evenly divided by another number, taken from the standart input.

 

So if this can be a task for 9th graders it should be easy...

 

What I did: array of chars which were dinamically allocated and used some loops to do basic operations. Also..You are free to overload operators like << to print directly.

Share this post


Link to post
Share on other sites

'int' doesn't hold 4294967295, 'unsigned int' does, int is signed by default and only holds 2147483647. (assuming the int is 32 bit - which isn't guaranteed  but likely).
 
If you include the standard header <cstdint>, C++ defines alot of integer types that are more specific:
int8_t = -127 to +127
uint8_t = 0 to 255
 
int16_t = -32,767 to +32,767 (32 thousand negative or positive)
uint16_t = 0 to 65,536 (65 thousand)
 
int32_t = -2,147,483,647 to +2,147,483,647    (two billion negative or positive)
uint32_t = 0 to 4,294,967,295  (4 billion)

int64_t = -9,223,372,036,854,775,807 to +9,223,372,036,854,775,807 (nine Quintillion negative or positive)
uint64_t = 0 to 18,446,744,073,709,551,615 (18 Quintillion)
 
If you need higher that 18 quintillion, you have to use a third-party library or roll your own, and it won't be as optimized.

 

My suggestions:

 

By default, use 'int' if you need a signed number, and 'unsigned int' (or just 'unsigned'; it means the same) if you need an unsigned number. These will be the best optimized.

If you actually need 32 bits, and not just a large number in general, use int32_t or uint32_t - they will be self-documenting your intent.

 

If you need to conserve memory, only then go lower to 16 bit or 8 bit integers - they can be slightly slower, but not something you'll notice. (Don't preoptimize, but have the knowledge).

If you need a larger number, use a 64 bit int.

If you need a extremely large number - use a BIGNUM class.

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

If you include the standard header , C++ defines alot of integer types that are more specific:
int8_t = -127 to +127

 

Actually int8_t is =-128 to 127 

uint8_t = 0 to 255
 
int16_t = -32,768 to +32,767 (32 thousand negative or positive)
uint16_t = 0 to 65,536 (65 thousand)
 
int32_t = -2,147,483,648 to +2,147,483,647    (two billion negative or positive)
uint32_t = 0 to 4,294,967,295  (4 billion)

int64_t = -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 (nine Quintillion negative or positive)
uint64_t = 0 to 18,446,744,073,709,551,615 (18 Quintillion)

Edited by BornToCode

Share this post


Link to post
Share on other sites
I wrote my own C++ bigint libraries. In fact I wrote three of them, one of which is used in a couple of commercial products, and has had extremely rigorous testing.
Bigint: for integers of a fixed (templated) size e.g. 512 bits, and comes in a signed and an unsigned variety.
VarBigInt: is for integers of a size that varies at runtime.
StringInt: holds integers as a string, for when displaying the value is more common than doing maths on it.

They're on my homepage here if you're curious to see how they are done: http://homepages.ihug.co.nz/~aurora76/Malc/Useful_Classes.htm
Be prepared to learn a lot about operator overloading if you look at them smile.png

There are limitations imposed by the C++ language mind you, in that you can't specifiy large numbers as you would normally do. Instead the typical thing for these libraries to do is to allow you to specify the value as a string. Other than that, they work pretty much like any other integral type.

Share this post


Link to post
Share on other sites


There are limitations imposed by the C++ language mind you, in that you can't specifiy large numbers as you would normally do. Instead the typical thing for these libraries to do is to allow you to specify the value as a string. Other than that, they work pretty much like any other integral type.

In C++11 you can use user defined literals for this.

Share this post


Link to post
Share on other sites

 

If you include the standard header , C++ defines alot of integer types that are more specific:
int8_t = -127 to +127

 

Actually int8_t is =-128 to 127 

 

Whoops, you're right. No point wasting a space for -0 is there? laugh.png

Having a negative zero could also introduce the need for different hardware paths for signed and unsigned arithmetic. One major advantage of two's complement notation is that signed and unsigned are exactly the same at hardware level. For addition, subtraction, and multiplication, anyway.

Share this post


Link to post
Share on other sites
It depends on your platform's negative number representation. Most modern machine will use 2's complement which will go from -128 to 127, but neither C nor C++ guarantee it. The C standard's SCHAR_MIN is listed as -127 not -128.

Share this post


Link to post
Share on other sites

If you include the standard header , C++ defines alot of integer types that are more specific:
int8_t = -127 to +127

Actually int8_t is =-128 to 127

As has been mentioned, on two's complement architectures, yes. 1's complement architectures do exist (although more "did"), having the advantage of slightly simpler circuitry. For practical purposes, nowadays, on commodity hardware, one generally assumes two's complement.

Edited by Geometrian

Share this post


Link to post
Share on other sites

There are limitations imposed by the C++ language mind you, in that you can't specifiy large numbers as you would normally do. Instead the typical thing for these libraries to do is to allow you to specify the value as a string. Other than that, they work pretty much like any other integral type.

In C++11 you can use user defined literals for this.
Yeah I'd read about those not long ago thanks. I'll try it out next time I have a compatible compiler installed.

Share this post


Link to post
Share on other sites

It depends on your platform's negative number representation. Most modern machine will use 2's complement which will go from -128 to 127, but neither C nor C++ guarantee it. The C standard's SCHAR_MIN is listed as -127 not -128.



If you include the standard header , C++ defines alot of integer types that are more specific:
int8_t = -127 to +127

Actually int8_t is =-128 to 127


As has been mentioned, on two's complement architectures, yes. 1's complement architectures do exist (although more "did"), having the advantage of slightly simpler circuitry. For practical purposes, nowadays, on commodity hardware, one generally assumes two's complement.


No, int8_t is 2's complement with no padding bits.
 
Servant of the Lord explicitly mentioned (and was corrected by BornToCode) using the u/intN_t types. The C standard states (which the C++ standard references in section 18.4.1 and requires to be compliant with the C standard):
 

7.20.1.1 Exact-width integer types
1 The typedef name intN_t designates a signed integer type with width N, no padding
bits, and a two’s complement representation. Thus, int8_t denotes such a signed
integer type with a width of exactly 8 bits.
2 The typedef name uintN_t designates an unsigned integer type with width N and no
padding bits. Thus, uint24_t denotes such an unsigned integer type with a width of
exactly 24 bits.
3 These types are optional. However, if an implementation provides integer types with
widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a
two’s complement representation, it shall de?ne the corresponding typedef names.

 
That is, these data types are required to be the specified number of bits in 2's complement representation. Section 7.20.2.1 confirms the ranges:
 

7.20.2.1 Limits of exact-width integer types
1 — minimum values of exact-width signed integer types
INTN_MIN exactly ?(2N?1)
— maximum values of exact-width signed integer types
INTN_MAX exactly 2N?1? 1
— maximum values of exact-width unsigned integer types
UINTN_MAX exactly 2N? 1

 
You are, of course, correct in that non-exact width integer types (like char, short, int, etc.) may not be 2's complement (and of course, the macros giving the min and max values for each type take this into account like you note with SCHAR_MIN/MAX). Edited by Cornstalks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement