PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] How to store 64 digits integer in C?

 April 28, 2022     c, long-long, warnings     No comments   

Issue

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.


Solution

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



Answered By - chux - Reinstate Monica
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing