Arbitrary size integers in C/C++

后端 未结 5 684
眼角桃花
眼角桃花 2020-12-31 21:52

Question

  • Is there a way to create a arbitrary size integer using c/c++?

For example:

int main(void) {
  Int i = Int(3); //3-bit i         


        
5条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 22:23

    Explanation:


    You could always try to use integer and float manipulation using arrays. If the number is too big for array initialization, you could use the malloc(); function.

    Please Note: This method is not very fast, because we will be allocating memory in the heap. You will also have to write your own mathematical operation functions, because I am not quite sure on how to implement it efficiently. SEE MORE BELOW


    How to implement (sort of):


    #include 
    #include 
    
    #define MAX_DIGIT_COUNT 1000
    
    int main()
    {
        int* big_num = (int*)malloc(sizeof(int) * MAX_DIGIT_COUNT); //allocate memory
        for(int x; x

    Please note: This is just a crude way of implementing arbitrary-sized number support in pure C.

提交回复
热议问题