加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何考虑简单操作的位操作?

发布时间:2020-12-16 10:38:51 所属栏目:百科 来源:网络整理
导读:例如: unsigned int a; // value to merge in non-masked bitsunsigned int b; // value to merge in masked bitsunsigned int mask; // 1 where bits from b should be selected; 0 where from a.unsigned int r; // result of (a ~mask) | (b mask) goes
例如:

unsigned int a;    // value to merge in non-masked bits
unsigned int b;    // value to merge in masked bits
unsigned int mask; // 1 where bits from b should be selected; 0 where from a.
unsigned int r;    // result of (a & ~mask) | (b & mask) goes here

r = a ^ ((a ^ b) & mask);

根据掩码合并来自两个值的位.

[取自here]

在这种情况下,我可以看到它有效,但我不确定逻辑是什么?而且我不确定我是否可以从头开始创建自己的位操作.我该如何开始思考?

解决方法

In this case,I can see that it works,but I am not sure what the logic is? And I am not sure I can create my own bit operations like this from scratch. How do I start thinking in bits?

人们回答了你的第一个问题 – 解释逻辑.我希望能向你展示一个非常基本的,冗长但标准的方法,使任何一点点操作. (注意,一旦你习惯了比特,你就会开始考虑&和|直接关闭而不做这样的废话).

>弄清楚你的操作要做什么.
>写出一个完整的真值表.
>直接从表中读取产品总和或制作卡诺图.公里将减少最终的公平.
> ???
>利润

得出你给出的例子.即,掩码从A或B中选择位.(0是A,1是B)

该表用于每个输入1位.我做的不多,因为我不想浪费我的时间:)(为什么?2 ^(2bits * 3inputs)= 64个案例:( 2 ^(3bits * 3inputs)= 512个案例:(( )

但好消息是,在这种情况下,操作独立于位数,因此1位示例是100%罚款.事实上,我推荐:)

| A | B | M || R |
============++====
| 0 | 0 | 0 || 0 |
| 0 | 0 | 1 || 0 |
| 0 | 1 | 0 || 0 |
| 0 | 1 | 1 || 1 |
| 1 | 0 | 0 || 1 |
| 1 | 0 | 1 || 0 |
| 1 | 1 | 0 || 1 |
| 1 | 1 | 1 || 1 |

希望你能看到这个真值表的工作原理.

如何从中得到一个表达式?两种方法:KMaps和副手.我们先做,我们先做吧?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读