简体   繁体   English

C 中两个数字的 32 位加法

[英]32 bit addition of two numbers in C

I have a code which performs the 32 bit addition of two numbers.I am generating 10 random 32 bit numbers and each of them are stored in two separate files as their corresponding hex values.The result is also stored in another file in the same way on the execution of the code i am getting some negative values for certain combinations.我有一个执行两个数字的 32 位加法的代码。我正在生成 10 个随机 32 位数字,每个数字都存储在两个单独的文件中作为它们对应的十六进制值。结果也以相同的方式存储在另一个文件中在执行代码时,我得到某些组合的一些负值。

Here is my code.这是我的代码。


    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #define TARGET_MAX 2147483647L
    void main ()
    {
        int32_t A[10] = {0};
        int32_t B[10] = {0};
        int32_t S[10] = {0};
        int i =0;
        int n =10;
        char hex[32] ={0};
        time_t t;
        FILE *fp = NULL;
        FILE *fp1 = NULL;
        FILE *fp2 = NULL;
        fp = fopen("A.txt","w");
        fp1 = fopen("B.txt","w");
        fp2 = fopen("S.txt","w");
        srand((unsigned) time(&t));
        for(i=0;i<n;i++)
        {
            A[i] = rand() % TARGET_MAX;
            if(fp != NULL)
            {   
                sprintf(hex,"%x",A[i]);
                fprintf(fp,"%s\n",hex);
                memset(hex,0,sizeof(hex));
            }
            B[i] = rand() % TARGET_MAX;
            if(fp1 != NULL)
                    {   sprintf(hex,"%x",B[i]);
                fprintf(fp1,"%s\n",hex);
                memset(hex,0,sizeof(hex));
                    }
            S[i] = A[i] + B[i];
            if(fp2 != NULL)
                    {   sprintf(hex,"%x",S[i]);
                fprintf(fp2,"%s\n",hex);
                memset(hex,0,sizeof(hex));
                    }
            printf(" %d + %d = %d \n\r",A[i],B[i],S[i]);
        }
        fclose(fp);
        fclose(fp1);
        fclose(fp2);
    }

Here is my output这是我的 output


    vu2swz@PPDP01:~/vlsi_lab/exp6$ gcc add.c -o add
    vu2swz@PPDP01:~/vlsi_lab/exp6$ ./add 
     1000086044 + 1204997665 = -2089883587 
     436835310 + 1696128436 = 2132963746 
     1624838244 + 335108562 = 1959946806 
     1782944281 + 1013582119 = -1498440896 
     1491331491 + 1257744454 = -1545891351 
     1676611730 + 773175875 = -1845179691 
     422206991 + 2136514004 = -1736246301 
     1622400103 + 152657712 = 1775057815 
     809410550 + 1662804335 = -1822752411 
     1396954314 + 742609108 = 2139563422 

You're running into overflow.你遇到了溢出。

In the instances where you're getting a negative number, the result is overflowing what can fix in a signed 32 bit integer.在您得到负数的情况下,结果是溢出可以在有符号的 32 位 integer 中修复的内容。

Since the numbers you're generating are smaller than 2 31 , their sum will be less than 2 32 which will fit in an unsigned 32 bit integer.由于您生成的数字小于 2 31 ,因此它们的总和将小于 2 32 ,这将适合无符号 32 位 integer。 So change your types to uint32_t and use the %u format specifier to print them.因此,将您的类型更改为uint32_t并使用%u格式说明符来打印它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM