如何在某个位置设置二进制形式的数字1或0

发布于 2024-12-07 03:44:48 字数 1095 浏览 0 评论 0原文

假设我有一个八位数字,我想在每个位位置设置数字1或0,这是动态情况。

假设这样的情况,用户输入两个相等或仅相差一的数字,我希望在从0位置到7的每次迭代中,以数字的二进制形式写入这些0和1,我如何在循环中实现它?请帮我。

一个例子:

int result = 0;

for (int i = 0; i < 8; i++) {
    int x, y;
    cin >> x >> y;

    if (x == y) {
        // set at i position 0;
    }

    else if ((x-y) == 1) {
        // set  at i position 1;(in result number)
    }
}

更新: 这就是我想要实现的: 将两个 8 位二进制补码相加 这是这个的代码

#include <iostream>
using namespace std;
int main(){
          int x,y;
          cin>>x>>y;
          int result=0;
          int carry=0;
         int sum=0;
          for (int i=0;i<8;i++){
              sum=carry;
           sum+= (x&(1<<i));
           sum+=(y&(1<<i));
              if (sum>1){
               sum-=2;
               carry=1;
              }
              else{



              carry=0;
              }
              result|=sum;
              result<<=1;



          }

           cout<<result<<" "<<endl;








 return 0;
}

Suppose I have an eight bit number, I want to set at each bit position number 1 or zero, it is dynamic situation.

Suppose for example such situation, user enters two numbers which are equal or differs only by one, and I want that at each iteration from 0 position to seven, write these 0 and 1 in binary form of number, how can I implement it in cycle? Please help me.

An example:

int result = 0;

for (int i = 0; i < 8; i++) {
    int x, y;
    cin >> x >> y;

    if (x == y) {
        // set at i position 0;
    }

    else if ((x-y) == 1) {
        // set  at i position 1;(in result number)
    }
}

updated :
it is what i want to implement :
Adding two 8-bit two's complement binary numbers
here is code for this

#include <iostream>
using namespace std;
int main(){
          int x,y;
          cin>>x>>y;
          int result=0;
          int carry=0;
         int sum=0;
          for (int i=0;i<8;i++){
              sum=carry;
           sum+= (x&(1<<i));
           sum+=(y&(1<<i));
              if (sum>1){
               sum-=2;
               carry=1;
              }
              else{



              carry=0;
              }
              result|=sum;
              result<<=1;



          }

           cout<<result<<" "<<endl;








 return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末的微笑 2024-12-14 03:44:48

您可以使用 AND 和 OR 二元运算符更改各个位。

例如:

//set first bit to 1
value |= 1;

//set fourth bit to 0
value &= ~(1<<3);

//set 6th bit to 1
value |= (1<<5);

You can change individual bits with AND and OR binary operators.

For example:

//set first bit to 1
value |= 1;

//set fourth bit to 0
value &= ~(1<<3);

//set 6th bit to 1
value |= (1<<5);
彡翼 2024-12-14 03:44:48

我不知道如果您的输入相差两个会发生什么,但您可能想要这样的东西:

int result = 0;

for (int i = 0; i < num_bits; ++i) {
    int a, b;
    std :: cin >> a >> b;

    result |= (a != b);
    result <<= 1;
}

I don't know what happens if your inputs are different by two but you might want something like this:

int result = 0;

for (int i = 0; i < num_bits; ++i) {
    int a, b;
    std :: cin >> a >> b;

    result |= (a != b);
    result <<= 1;
}
凉栀 2024-12-14 03:44:48

考虑位移位。

设置该位:

result |= (1<<i);

取消设置该位留给读者作为练习。

Consider bit shifting.

To set the bit:

result |= (1<<i);

Un-setting the bit is left as an excercise to the reader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文