• Create Account

### #ActualServant of the Lord

Posted 07 February 2013 - 12:51 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.

### #2Servant of the Lord

Posted 07 February 2013 - 12:50 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.

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.

### #1Servant of the Lord

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.

PARTNERS