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.
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.
Posted 07 February 2013 - 06:31 AM
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.
Posted 07 February 2013 - 09:06 AM
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.
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.
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.
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.
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 - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
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.
Posted 07 February 2013 - 02:40 PM
Posted 07 February 2013 - 04:32 PM
In C++11 you can use user defined literals for this.
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.
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? ![]()
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 - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
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.
Posted 07 February 2013 - 07:01 PM
Actually int8_t is =-128 to 127If you include the standard header , C++ defines alot of integer types that are more specific:
int8_t = -127 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.
Posted 07 February 2013 - 07:53 PM
Yeah I'd read about those not long ago thanks. I'll try it out next time I have a compatible compiler installed.In C++11 you can use user defined literals for this.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.
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.
No, int8_t is 2's complement with no padding bits.
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.
Actually int8_t is =-128 to 127If you include the standard header , C++ defines alot of integer types that are more specific:
int8_t = -127 to +127
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 define the corresponding typedef names.
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
Edited by Cornstalks, 07 February 2013 - 08:57 PM.