C位运算从一个字节/复制一个位到另一个字节 [英] C bit operations / copy one bit from one byte to another byte
本文介绍了C位运算从一个字节/复制一个位到另一个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何设置了一下,清除了一下,拨动了一下,检查是否置位。
但是,我怎么能在byte_2 NR byte_1的7位拷贝,例如位NR 7?
这是可能没有一个if语句(不检查位的值)?
的#include<&stdio.h中GT;
#包括LT&;&stdint.h GT;
诠释主(){
INT byte_1 = 0b00001111;
INT byte_2 = 0b01010101; byte_2 = //下一步是什么? 返回0;
}
解决方案
byte_2 =(byte_2&安培; 0b01111111)| (byte_1&安培; 0b10000000);
I know how to set a bit, clear a bit , toggle a bit, and check if a bit is set.
But, how I can copy bit, for example nr 7 of byte_1 to bit nr 7 in byte_2 ?
It is possible without an if statement (without checking the value of the bit) ?
#include <stdio.h>
#include <stdint.h>
int main(){
int byte_1 = 0b00001111;
int byte_2 = 0b01010101;
byte_2 = // what's next ?
return 0;
}
解决方案
byte_2 = (byte_2 & 0b01111111) | (byte_1 & 0b10000000);
这篇关于C位运算从一个字节/复制一个位到另一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文