简体   繁体   中英

Bit Operation in C / C++

When we talk Bit Operation in C or C++. Does bit start from bit0 or bit1 ? Which one is more make sense? As I know A bit can assume either of two values: 1 or 0.

Generally, bit identifiers start from 0 at the least significant end, such as with the following octet:

+----+----+----+----+----+----+----+----+
| b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |
+----+----+----+----+----+----+----+----+
  80   40   20   10   08   04   02   01    <-- hex value

While a bit can take either a 0 or 1 value, that doesn't limit their identifiers, which can range from zero up to the number of bits minus 1.

For an explanation of the bitwise operators, see here .

For example, if you wanted to know whether b3 was set in C:

b3 = value & 0x08; // non-zero if set.

Similarly, setting b0 and clearing b7 can be done with:

value = (value | 0x01) & 0x7f; // or with 0000-0001, and with 0111-1111.

我们总是从位0开始,几乎总是最低有效位。

按照惯例,位索引从0开始,例如对于(x >> i) & 1类的表达式。

its not Bit Operations, but Bitwise Operations

A bitwise operation is performed on all the bits of a variable, eg

1 XOR 2

for integers of size 2 byte mean

0000 0000 0000 0001 XOR 0000 0000 0000 0010

位操作使用操作数中的所有位。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM