0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nano HE
  • 9,109
  • 31
  • 97
  • 137
  • 1
    Can you rephrase this question *"Does bit start from bit0 or bit1?"*? What does it mean? – Nawaz Mar 15 '11 at 02:15
  • @Nawaz, It's a question about the index (0 or 1 )for bit in common question. – Nano HE Mar 15 '11 at 02:18
  • How does it matter? Indexing bit doesn't make sense. You cannot access it using index, after all. Indexing makes sense only in case of arrays. But here you can assume anything, bit0 or bit1 or whatever, as long as you're consistent throughout! – Nawaz Mar 15 '11 at 02:21
  • @Nawaz, Actually I added a comment line to my code. I used `&` in my bit operation statement. But I don't know how can I commented it. Thank you. – Nano HE Mar 15 '11 at 02:26
  • 1
    You can also use the phrase *"first least significant bit"* to indicate the rightmost bit. – Nawaz Mar 15 '11 at 02:28

5 Answers5

7

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.
Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

We always start from bit 0, which is almost always the least-significant bit.

Gabe
  • 84,912
  • 12
  • 139
  • 238
1

By convention bit indexing starts at 0, e.g. for an expression such as (x >> i) & 1.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

its not Bit Operations, but Bitwise Operations

A bitwise operation is performed on all the bits of a variable, e.g

1 XOR 2

for integers of size 2 byte mean

0000 0000 0000 0001 XOR 0000 0000 0000 0010

AbiusX
  • 2,379
  • 20
  • 26
0

Bit operations use all the bits in the operands.

user623879
  • 4,066
  • 9
  • 38
  • 53