C ++中的按位运算符 [英] Bitwise Operators In C++

查看:106
本文介绍了C ++中的按位运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    unsigned char y = 0x0;
y = y | 1; //Set Bit 0
y = y | 8; //Set Bit 3
y = y &~1; //Clear Bit 0
y = y &~8; //Clear Bit 3



我正在阅读按位操作,并且被一件事弄糊涂了,`y = y | 1`将Oth位设置为1,如何将第1位设置为1?你怎么做到最后?另外你如何决定哪个数字设置哪个位?



谢谢。


I was reading on bitwise op's, and got confused by one thing, `y = y | 1` sets the Oth bit to 1, how do you set the 1st bit to one? How do you do the last? Also how do you decide which number sets which bit?

Thanks.

推荐答案

简单。

Simple.
y = y | 2;   //set Bit 1
y = y | 128; // set Bit 7



考虑前几位数字的二进制表示。

0 = 0000 0000

1 = 0000 0001 *

2 = 0000 0010 *

3 = 0000 0011

4 = 0000 0100 *

5 = 0000 0101

6 = 0000 0110

7 = 0000 0111

8 = 0000 1000 *



注意标有*的那些,它们是那些只有1位设置。这些是用于设置/清除特定位的数字。另请注意,* ed项目的所有权力均为2?您现在可能已经猜到,您可以使用数字16,32,64,128来调整8位变量的其他位。



十六进制数字是在处理位域时,通常比十进制更容易使用。这是因为每个半字节或十六进制数字相当于4位。

这将为您提供以下数字来设置/清除8位数字中的位:

( bit 0)0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80(bit 7)

更容易看出上面的每个数字都有一个位设置一个是它,而不是它们的十进制等值。


Consider the binary representation of the first few digits.
0 = 0000 0000
1 = 0000 0001 *
2 = 0000 0010 *
3 = 0000 0011
4 = 0000 0100 *
5 = 0000 0101
6 = 0000 0110
7 = 0000 0111
8 = 0000 1000 *

Notice the ones marked with a *, they're the ones that have only 1 bit set. These are the numbers you use to set/clear a particular bit. Notice also, that the *ed items are all powers of 2? You have probably guessed by now that you can fiddle with the other bits of an 8 bit variable by using the numbers 16, 32, 64, 128.

Hexidecimal numbers are often easier to work with than decimal ones when dealing with bit-fields. This is because each nibble or hexadecimal digit is equivalent to 4 bits.
This would leave you with the following numbers to set/clear bits in an 8 bit number:
(bit 0)0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80(bit 7)
It's much easier to see at a lance that each of the above numbers have one bit set and which one it is, than it would be with their decimal equivalents.


你认真吗?它是 2 == 1<< 1

2 N ,其中N = 0,1,2 ......表示一个位集,其中N-the位置1,所有其他位清零。这些数字是

Are you serious? It's 2 == 1 << 1.
2N, where N = 0, 1, 2… presents a bitset where N-the bit is set and all other bits are clear. Those numbers are
1 << 0 == 1,
1 << 1 == 2,
1 << 2 == 4,
...
1 << N...

我甚至不明白这里有什么需要解释。这是你需要完全理解的数学,计算机科学和实用编程的基础之一,否则你在编程中根本无法做任何事情。



参见:

https://en.wikipedia.org/wiki/Binary_number [ ^ ],

https://en.wikipedia.org/wiki/Binary_code [ ^ ]。



-SA

I don't even understand what needs explanation here. This is one of the fundamentals of mathematics, computers science and practical programming you need to understand perfectly, otherwise you cannot do anything at all in programming.

See also:
https://en.wikipedia.org/wiki/Binary_number[^],
https://en.wikipedia.org/wiki/Binary_code[^].

—SA


一切都在按位运算的含义。



在cpu中,整数以二进制形式存储。这意味着每个位保持2的幂的值。

位0的值为2 ^ 0,位1的值为2 ^ 1,依此类推。
$ b $ 10(基数10) 1010 (基数2)

1 * 2 ^ 3 + 0 * 2 ^ 2 + 1 * 2 ^ 1 + 0 * 2 ^ 0



按位意味着操作应用于每个位而没有位之间的链接(无进位)。

结果的位n是应用于操作数的位n的操作的结果。



按位运算的一个优点是并行处理,每个操作并行应用于每个操作数位。
Everything is in bitwise operation meaning.

In the cpu, integers are stored in a binary form. it means that each bit hold the value of a power of 2.
bit 0 has the value of 2^0, bit 1 has the value of 2^1 and so on.
10 (base 10) is 1010 (base 2)
which is 1*2^3+ 0*2^2+ 1*2^1+ 0*2^0

Bitwise means that the operation is applied to each bit without link between bits (no carry).
Bit n of the result is the result of the operation applied to bit n of the operands.

One advantage of bitwise operations is parallel treatment, each operation is applied to each bit of the operands in parallel.


这篇关于C ++中的按位运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆