简体   繁体   English

如何更改 32 位整数的字节?

[英]How do I change the bytes of a 32-bit integer?

I'm trying to add two binary numbers together using only logical statements and binary arithmetic operators.我试图仅使用逻辑语句和二进制算术运算符将两个二进制数加在一起。 But I'm confused on how to actually change the bits.但我对如何实际更改位感到困惑。 It is mostly the out variable that I am trying to change, but it keeps getting zeroed every time I print it.它主要是我试图更改的 out 变量,但每次打印时它都会变为零。

#include <stdio.h>

void execute_add(int a, int b){

int i = 0;
int bit;
int bit2;
int carryOut = 0;
int out = 10;
int overflow = 0;

for(i = 0; i <32 ; i++){

  bit = (a >> i) & 1;
  bit2 = (b >> i) & 1;

  if(bit==1 && bit2==1 && carryOut == 0){
        carryOut = 1;
        out = 0 | (0x1 >> i);

  }else if(bit==1 && bit2==1 && carryOut == 1){
        carryOut = 1;
        out = 1 | (0x1 >> i);

  }else if(bit==0 && bit2==0 && carryOut == 0){
        carryOut = 0;
        out= 0 | (0x1 >> i);

  }else if(bit==0 && bit2==0 && carryOut == 1){
        carryOut = 0;
        out = 1 | (0x1 >> i);

  }else if(bit==1 && bit2==0 && carryOut == 0){
        carryOut = 0;
        out = 1 | (0x1 >> i);

  }else if(bit==1 && bit2==0 && carryOut == 1){
        carryOut = 1;
        out = 0 | (0x1 >> i);

  }else if(bit==0 && bit2==1 && carryOut == 0){
        carryOut = 0;
        out = 1 | (0x1 >> i);

  }else if(bit==0 && bit2==1 && carryOut == 1){
        carryOut = 1;
        out = 0  | (0x1 >> i);

  }else{

  }//if else

 }//for loop

   printf("\n");

   bit = (a >> 31) & 1;
   bit2 = (a >> 31)& 1;
   int bit3 = (out >> 31) & 1;

   if( bit == 1 && bit2== 1 && bit3 == 0){
     overflow = 1;

   }else if (bit == 0 && bit2 == 0 && bit3 == 1){
   overflow = 1;

   }else{

   }//overflow check

    int j;
    int g = 0;

    for(j = 31; j>=0; j--){

            if(g%4==0 && g!=0){
             printf(" ");
            }

        bit2 = (out >> j) & 1;

        printf("%d", bit2);
        g++;
    }

            printf("\n");
}

int main (){

  int a = 34;
  int b = 17;

  execute_add(a, b);
  return 0;
}

With each of these statements in your for loop:在 for 循环中使用这些语句中的每一个:

out = 0 | x;

You're resetting out , and clearing away all the work you've already done.你重置out ,并且清除了所有你已经完成的工作。 You probably mean to do:你可能的意思是:

out = out | x

Or, equivalently,或者,等效地,

out |= x

You also are right-shifting 1 all over the place, which is not what you're looking for;您还在整个地方右移1 ,这不是您要找的; for any shift greater than zero, that's going to give you zero.对于任何大于零的移位,这将给您零。 I think you're often looking for a left-shift where you use a right shift.我认为您经常在使用右移的情况下寻找左移。

I would second kaylum's comment about using a debugger;我想第二个 kaylum 关于使用调试器的评论; even if you flip the appropriate shifts and |即使您翻转适当的班次和| with out properly, you're still going to have logic errors that will be easily fixed with a debugger.如果out正确,您仍然会遇到逻辑错误,这些错误可以通过调试器轻松修复。

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

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