How to store 64 digits integer in C?

一曲冷凌霜 提交于 2020-05-23 07:23:33

问题


I am dealing with 2 64 digits integers and need to multiple these two numbers. When I tried to store it into a long long int variable, I got the following compiling error:

1.c: In function ‘main’:
1.c:5:6: warning: integer constant is too large for its type.
a = 1234567890123456789012345678901234567890123456789012345678901234;

Can someone tell me how to store the integer in C?

[edit] OP later implies a 64 decimal digit number.


回答1:


1234567890123456789012345678901234567890123456789012345678901234 is not a 64-bit number. It is a 64 decimal digit number needing about 210+ binary bits to be stored.

Try storing a 64-bit number

long long s1 = 9223372036854775807;
long long s2 = -9223372036854775807 - 1;
unsigned long long u1 = 18446744073709551615u;

To store a 64-digit decimal number in standard C, you need to use another approach as C's integer types are specified for only up to 64 binary digits (bits) although wider ones could exist: Store as your own array of digits, as a string, or use a bignum library like gmp. It depends on what you want to do with that stored 64-decimal digit number.


Example string approach. It lacks buffer protection, nor removes leading zeros and is not so efficient. It does demonstrate the flow of what is needed - basic long multiplication.

char *string_mult2(char *product, const char *a, const char *b) {
  size_t alen = strlen(a);
  size_t blen = strlen(b);
  size_t clen = alen + blen;
  memset(product, '0', clen);
  product[clen] = 0;
  for (size_t ai = alen; ai-- > 0;) {
    unsigned acc = 0;
    size_t ci = --clen;
    for (size_t bi = blen; bi-- > 0;) {
      acc += product[ci] - '0' + (a[ai] - '0') * (b[bi] - '0');
      product[ci--] = acc % 10 + '0';
      acc /= 10;
    }
    product[ci] = acc % 10 + '0';
  }
  return product;
}

int main(void) {
  char *a = "1234567890123456789012345678901234567890123456789012345678901234";
  //a = "12";
  char *b = a;
  char product[200];
  puts(string_mult2(product,a,b));
  return 0;
}

Output

After you tried compiling the code and ran it, mouse over the below to see my result.

01524157875323883675049535156256668194500838287337600975522511810828928529615005335814711781866792303015211342784374345526722756




回答2:


This multiplies two 64-decimal-digit integers using the GNU multiple precision arithmetic library

example.c

#include <gmp.h>
#include <stdio.h>

int main(void)
{
  // A 64-digit number expressed as a string
  const char str[] = "1234567890123456789012345678901234567890123456789012345678901234";

  mpz_t n;                         // mpz_t is the type defined for GMP integers
  mpz_init(n);                     // Initialize the number

  mpz_set_str(n, str, 10);         // parse the string as a base-10 number

  mpz_mul(n, n, n);                // square the number (n = n * n)

  printf("n * n = ");              // print the result
  mpz_out_str(stdout, 10, n);

  return 0;
}

Compile and link with the GMP library

gcc -lgmp example.c -o example

Output

n * n = 15241578753238836750495351562566681945008382873376009755225118122311263526910001524158887639079520012193273126047859425087639153757049236500533455762536198787501905199875019052100

Reference

  • GNU MP Homepage
  • GNU MP Manual
  • Tutorial on GMP



回答3:


A long long int or an int64_t will suffice for a 64 bit integer. However, the max 64 bit integer is 9,223,372,036,854,775,807, while your number is bigger than that, thus a 128-bit integer could be used, but still I am afraid this will not be enough.

GCC does have an uint128_t/int128_t type, starting from version 4. Read more in Is there a 128 bit integer in gcc?



来源:https://stackoverflow.com/questions/44576430/how-to-store-64-digits-integer-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!