-2

I have a 20x30 array which contains values of int type. Each row should be concentrated into one number. For example, consider a 3x4 array: |1 3 6 1| |4 2 5 2| |8 3 1 5| It should become: |1361, 4252, 8315| The problem is that, I have 30 numbers so I need 30 digits, and the biggest number I can store using unsigned long long int is +18,446,744,073,709,551,615 which contains only 20 digits

ORIGINAL PROBLEM: The previous problem is a part of an assignment, where I have a hash function which returns a Nx30 array. Each row of the array consists of 30 integers and putting them together, represents a 30digit hash value of one of the N input data.

I have to sort those hashes using radix sort. My instructor told me that i can match those 30 digits in a 64bit integer. Given that the hash values are in range of 0-3, can I concentrate them, using 2 bits for each one, in a 64bit array?

Dimitris
  • 7
  • 4
  • 3
    So, you want to store 30 integers in one integer? Even if the first 30 are 32 bit rather than 64, it's still not possible. Providing more detail of the problem and a code sample of what you'd like to achieve may help us guide you towards a better solution. – OMGtechy Aug 03 '14 at 22:33
  • 1
    Are all of your integers single-digit? – merlin2011 Aug 03 '14 at 22:35
  • You'll have to use an array of `uint32_t` or `uint64_t`, if they exist in `stdint`. – Fiddling Bits Aug 03 '14 at 22:35
  • Seems like you can use as large an int as you can find, so can you make an Arbitrary sized int (add ints as you need them)? – SingleStepper Aug 03 '14 at 22:41
  • 7
    It takes 100 bits to store 30 digits in the range 0..9. Trivial math: 10^30 = 2^99.6578. The only way forward is to limit what the digits can be. If you have only 4 values, say 0..3, then 4^30 = 2^60. That's the best you'll do. – Gene Aug 03 '14 at 22:44
  • 4
    As put, the problem has no solution. If we knew why you wanted to do this, maybe there would be a way. – david.pfx Aug 03 '14 at 22:58

1 Answers1

2

You need more than 64 bits to store 30 single-digit integers.

The C language doesn't have built-in numeric types bigger than 64 bits. Some compilers support them though. IMHO you're better off using GMP, or another library that supports it rather than relying on a specific compiler. As a last resource, you might want to implement it on your own, which isn't a very trivial task.

jweyrich
  • 31,198
  • 5
  • 66
  • 97
  • Is `uint128_t` a compiler extension and not part of the standard (e.g. in `stdint`)? – Fiddling Bits Aug 03 '14 at 23:39
  • @FiddlingBits: C99 and C11 don't provision numeric types wider than 64 bits. If you're using GCC for example, you can either use `__uint128_t ` or define your own 128-bit integer type using `__attribute__((mode(TI)))`. – jweyrich Aug 04 '14 at 00:21
  • @jweyrich: The both standards mention *extended integer types*, but it's only a possibility rather than requirement. Interestingly GCC documentation [claims](http://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html) (though not directly), that `__uint128_t` is not EIT. – Grzegorz Szpetkowski Aug 04 '14 at 06:32