• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Beyond 4294967295

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

16 replies to this topic

### #1Benderwiz  Members

Posted 07 February 2013 - 06:19 AM

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?

### #2Milcho  Members

Posted 07 February 2013 - 06:31 AM

POPULAR

There's actually a lot of third party libraries that support large numbers, though for obvious reasons (they're at least partially software implemented, instead of fully hardware implemented), they are slower.

Just googling a big integer library: https://mattmccutchen.net/bigint/ - can (according to the author) handle ints only limited by your computer's memory.

It's not particularly difficult to write a basic big int class - it's a common exercise in a lot of programming books when teaching classes and overloading operators.

### #3Cornstalks  Members

Posted 07 February 2013 - 09:06 AM

POPULAR

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

To answer your question: no. If you write 4294967295 in binary it's: 0b11111111111111111111111111111111. There are 32 ones in that binary number. It needs 32 bits, all of them, to represent that number. If you try to go higher, even by one, you need more bits. 32 won't be enough anymore. You can't magically pull more bits out of thin air. You get more bits by either using a larger data type or a custom big integer library (like Milcho said), but it isn't possible to magically add more bits to your int.

If you want to know just how big a number a datatype can hold, you can either google your compiler's documentation, or you can use std::numeric_limits:

#include <iostream>
#include <limits>

int main()
{
std::cout << std::numeric_limits<int>::max() << std::endl; // this will tell you the max for int... it is the *max*... ints *cannot* got higher than this
std::cout << std::numeric_limits<unsigned int>::max() << std::endl; // like above, but for unsigned ints.
}


Edited by Cornstalks, 07 February 2013 - 09:10 AM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #4KnolanCross  Members

Posted 07 February 2013 - 09:44 AM

You can use an unsigned long long int. If you need more than that, you will need to use a big number library.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

### #5Nickie  Members

Posted 07 February 2013 - 09:50 AM

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.

### #6SiCrane  Moderators

Posted 07 February 2013 - 10:59 AM

Another library option is GMP.

### #7Matias Goldberg  Members

Posted 07 February 2013 - 12:32 PM

If you use __int64 your max number now becomes 18.446.744.073.709.551.615 which is pretty large. If you need more than that (or an arbitrary representation until you've exhausted RAM), you'll have to use a big num library

### #8Servant of the Lord  Members

Posted 07 February 2013 - 12:44 PM

'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, 07 February 2013 - 12:51 PM.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #9BornToCode  Members

Posted 07 February 2013 - 02:35 PM

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, 07 February 2013 - 02:38 PM.

### #10iMalc  Members

Posted 07 February 2013 - 02:40 PM

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

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 order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

### #11King Mir  Members

Posted 07 February 2013 - 04:32 PM

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.

### #12Servant of the Lord  Members

Posted 07 February 2013 - 05:38 PM

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?

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #13Bacterius  Members

Posted 07 February 2013 - 06:54 PM

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?

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #14SiCrane  Moderators

Posted 07 February 2013 - 06:57 PM

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.

### #15Geometrian  Members

Posted 07 February 2013 - 07:01 PM

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, 07 February 2013 - 07:01 PM.

And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.

### #16iMalc  Members

Posted 07 February 2013 - 07:53 PM

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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

### #17Cornstalks  Members

Posted 07 February 2013 - 08:44 PM

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, 07 February 2013 - 08:57 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.