程序:
typedef bitset<8> bits;
char original = 0xF0F0F0F0;
char Mask = 0xFFFF0000;
char newBits = 0x0000AAAA;
/*& operation with "0bit set 0" & "1bit give no change to original byte" */
cout<<"Original o: "<<bits(original)<<endl;
cout<<"NewBits: "<<bits(newBits)<<endl;
cout<<"Mask m: "<<bits(Mask)<<endl;
cout<<"o & m with Mask: "<<bits(original & Mask)<<endl;/*0 set original bit as 0 */
结果:
Original o: 11110000
NewBits: 10101010
Mask m: 00000000
o & m with Mask: 00000000
Result 10101010
我理解十六进制及其结果..但是....... o & m
== 0000 0000
所以 bits(o & m | newBits)
结果应该是 0000 0000
,而不是 1010 1010
...
我错过了这个概念......
谁能帮帮我...
期待一个好的回应
谢谢
最佳答案
o & m = 0000 0000
和 newBits = 1010 1010
。因此,如果您对它们进行或运算(按位),您将得到 1010 1010
作为 0|0=0, 0|1=1, 1|0=1, 1|1=1 的结果
。
0000 0000 OR WITH
1010 1010
-----------------
1010 1010
-----------------
关于c++ - 使用位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5645852/