无符号幅度和二进制补码

我已经编写了一个代码来计算给定二进制代码的无符号幅度和二进制补码,除了我没有得到我应该的值。 我不确定我的代码究竟出了什么问题。 我希望不要在我的代码中使用幂函数和乘法函数。 我在下面发布了我的代码,请让我知道为什么我得到的值不正确。

#include "stdio.h" #define MAX_BITS 32 #define ENTER '\n' #define NUMBER_TWO 2 int main() { int unsignedMag; int twosComp; int negation[MAX_BITS]; int bitStore[MAX_BITS]; char enter; int count; //Input from the User printf("Enter up to 32 bits (hit 'enter' to terminate early): "); //Reads the first bit as a character char bit = getchar(); count = 0; while (count < MAX_BITS && (bit == '0' || bit == '1')) { bit = bit - '0'; bitStore[count++] = bit; bit = getchar(); } //Terminates if user hits enter if (bit == enter) { return 0; } //Continue through code else { //Loop to calculate unsigned magnitude for (int i = 0; i < count; i++) { unsignedMag = unsignedMag + unsignedMag + bitStore[i]; } printf("Unsigned magnitude: "); printf("%d", unsignedMag); printf("\n"); //Loop to calculate complete negation for (int j = 0; j < bitStore[j]; j++) { negation[j] = ~bitStore[j]; } negation[0] = negation[0] + 1; for (int l = 0; l < count; l++) { twosComp = twosComp + (negation[l] + (l *= NUMBER_TWO)); } if (bitStore[0] == '1') { printf("Twos Complement: -"); printf("%d", twosComp); printf("\n"); } else { printf("Twos Complement: "); printf("%d", twosComp); printf("\n"); } } return 0; } 

您的代码中存在多个问题。 编译并启用警告显示7个问题:

 smag.c:19:16: warning: implicit conversion loses integer precision: 'int' to 'char' [-Wconversion] char bit = getchar(); ~~~ ^~~~~~~~~ smag.c:24:15: warning: implicit conversion loses integer precision: 'int' to 'char' [-Wconversion] bit = getchar(); ~ ^~~~~~~~~ smag.c:50:53: warning: unsequenced modification and access to 'l' [-Wunsequenced] twosComp = twosComp + (negation[l] + (l *= NUMBER_TWO)); ~ ^ smag.c:4:9: warning: macro is not used [-Wunused-macros] #define ENTER '\n' ^ smag.c:50:24: warning: variable 'twosComp' may be uninitialized when used here [-Wconditional-uninitialized] twosComp = twosComp + (negation[l] + (l *= NUMBER_TWO)); ^~~~~~~~ smag.c:9:17: note: initialize the variable 'twosComp' to silence this warning int twosComp; ^ = 0 smag.c:36:27: warning: variable 'unsignedMag' may be uninitialized when used here [-Wconditional-uninitialized] unsignedMag = unsignedMag + unsignedMag + bitStore[i]; ^~~~~~~~~~~ smag.c:8:20: note: initialize the variable 'unsignedMag' to silence this warning int unsignedMag; ^ = 0 smag.c:28:16: warning: variable 'enter' is uninitialized when used here [-Wuninitialized] if (bit == enter) { ^~~~~ smag.c:12:15: note: initialize the variable 'enter' to silence this warning char enter; ^ = '\0' 7 warnings generated. 

即:

  • bit应定义为int
  • twosCompunsignedMag未初始化,它们应初始化为0
  • enter应该是ENTER ,但测试真的应该是:

     if (count == 0) return 1; 
  • 目前还不清楚你在计算twosComp想要完成什么,但是表达式twosComp = twosComp + (negation[l] + (l *= NUMBER_TWO)); 具有未定义的行为,因为您修改l并在表达式中使用其值两次。

  • 标准标题应拼写为尖括号:

     #include  
  • if在发布的代码中使用空白行和注释,则很容易将else分支为其分隔。 完全删除else因为if返回给调用者。

  • 在计算否定时 ,要注意negation[j] = ~bitStore[j]; 产生的值不是位,而是除最后一位之外的所有位一位或全位。 你应该写:

      negation[j] = bitStore[j] ^ 1;