sharan
sharan

Reputation: 226

Is there a way to increase the size of an int in C++ without using long?

If the range of int is up to 32768, then I have to input a value of around 50000 and use it,I want to input it without using long and if possible without using typecasting also. Is there any way to do it. I want the datatype to remain int only.

Upvotes: 0

Views: 5741

Answers (6)

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16039

Here is an idea to actually store values larger than MAX_INT in an int. It is based on the condition that there is only a small, known number of possible values.

You could write a compression method which computes something akin to a 2-byte hash. The hashes would have to have a bijective (1:1) relation to the known set of possible values. That way you would actually store the value (in compressed form) in the int, and not in a string as before, and thus expand the range of possible values at the cost of not being able to represent every value within that range.

The hashing algorithm would depend on the set of possible values. As a simple example let's assume that the possible values are 2^0, 2^1, 2^2... 2^32767. The obvious hash algorithm is to store the exponent in the int. A stored value of 4 would represent the value 16, 5 would represent 32, 1000 would represent a number close to 10^301 etc. One can see that one can "store" extraordinarily large numbers in a 16 bit int ;-). Less regular sets would require more complicated algorithms, of course.

Upvotes: 0

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16039

Several possibilities come to mind.

  • @abcthomas had the idea to use unsigned; since you are restricted to int, you may abuse int as unsigned. That will probably work, although it is UB according to the standard (cf. Integer overflow in C: standards and compilers).
  • Use two ints. probably involves writing your own scanf and printf versions, but that shouldn't be too hard. Strictly spoken though, you still haven't expanded the range of an int.
  • [Use long long] Not possible since you must use int.
  • You can always use some big number library. Probably not allowed either.
  • Keep the numbers in strings and do arithmetic digit-wise on the strings. Doesn't use int though.

But you'll never ever be able to store something > MAX_INT in an int.

Upvotes: 1

sashoalm
sashoalm

Reputation: 79595

No!


An int depends on the native machine word, which really means it depends on 3 things - the processor, the OS, and the compiler.

The only way you can "increase" an int foo; (not a long foo;, int is not a long) is:

  1. You are compiling with Turbo-C or a legacy 16-bit DOS compiler on a modern computer, likely because your university requires you to use that, because that's what your professor knows. Switch the compiler. If your professor insists you use it, switch the university.

  2. You are compiling with a 32-bit compiler on a 64-bit OS. Switch the compiler.

  3. You have 32-bit OS on a 64-bit computer. Reinstall a 64-bit OS.

  4. You have 32-bit processor. Buy a new computer.

  5. You have a 16-bit processor. Really, buy a new computer.

Upvotes: 1

nalinc
nalinc

Reputation: 7425

You may try unsigned int. Its same as int but with positive range(if you really dont want to use long).

see this for the range of data types

suggestion: You might aswell consider switching your compiler. From the range you've mentioned for int, it seems you are using a 16 bit compiler(probably turbo c). A 16-bit compiler would restrict unsigned int range to 0-65536(2^16) and signed int to –32,768 to 32,767.

Upvotes: 1

stefan
stefan

Reputation: 10355

Any built-in type cannot be altered nor expanded in any sense. You have to switch to a different type.

The type int has the following requirements:

  • represents at least the range -32767 to 32767 (16bit)
  • is at least as large as short (sizeof(short) <= sizeof(int))

This means, that strictly speaking (although most platforms use at least 32bit for int), you can't safely store the value 50000 in an int.

If you need a guaranteed range, use int16_t, int32_t or int64_t. They are defined in the header <cstdint>. There is no arbitrary precision integer type in the language or in the standard library.

If you only need to observe the range of valid integers, use the header <limits>:

std::cout << std::numeric_limits<int>::min() << " to " << std::numeric_limits<int>::max() << "\n";

Upvotes: 3

notadam
notadam

Reputation: 2884

Try splitting up your value (that would fit inside a 64-bit int) into two 32-bit chunks of data, then use two 32-bit ints to store it. A while ago, I wrote some code that helped me split 16-bit values into 8-bit ones. If you alter this code a bit, then you can split your 64-bit values into two 32-bit values each.

#define BYTE_T uint8_t
#define TWOBYTE_T uint16_t
#define LOWBYTE(x)          ((BYTE_T)x)
#define HIGHBYTE(x)         ((TWOBYTE_T)x >> 0x8)
#define BYTE_COMBINE(h, l)  (((BYTE_T)h << 0x8) + (BYTE_T)l)

I don't know if this is helpful or not, since it doesn't actually answer your original question, but at least you could store your values this way even if your platform only supports 32-bit ints.

Upvotes: 0

Related Questions