How to implement big int in C++

前端 未结 13 1890
攒了一身酷
攒了一身酷 2020-11-22 07:48

I\'d like to implement a big int class in C++ as a programming exercise—a class that can handle numbers bigger than a long int. I know that there are several open sou

相关标签:
13条回答
  • 2020-11-22 08:01

    Like others said, do it to old fashioned long-hand way, but stay away from doing this all in base 10. I'd suggest doing it all in base 65536, and storing things in an array of longs.

    0 讨论(0)
  • 2020-11-22 08:03

    addition would probably have to be done in the standard linear time algorithm
    but for multiplication you could try http://en.wikipedia.org/wiki/Karatsuba_algorithm

    0 讨论(0)
  • 2020-11-22 08:06

    Don't forget that you don't need to restrict yourself to 0-9 as digits, i.e. use bytes as digits (0-255) and you can still do long hand arithmetic the same as you would for decimal digits. You could even use an array of long.

    0 讨论(0)
  • 2020-11-22 08:06

    subtract 48 from your string of integer and print to get number of large digit. then perform the basic mathematical operation . otherwise i will provide complete solution.

    0 讨论(0)
  • 2020-11-22 08:07

    Once you have the digits of the number in an array, you can do addition and multiplication exactly as you would do them longhand.

    0 讨论(0)
  • 2020-11-22 08:08

    If your target architecture supports BCD (binary coded decimal) representation of numbers, you can get some hardware support for the longhand multiplication/addition that you need to do. Getting the compiler to emit BCD instruction is something you'll have to read up on...

    The Motorola 68K series chips had this. Not that I'm bitter or anything.

    0 讨论(0)
提交回复
热议问题