Reputation: 1308
For this example, I create an integer.
int example = 0;
Now let's say I want to know what the first bit of this integer is. I know it would be at bit position 0. But would I call it bit 1 or 0. The reason I ask is because I have seen documentation where the first bit of an integer is labeled as bit 0 and then later labeled as bit 1. I know it's a mistake on their end, just curious as to what I should be referring to it as.
Upvotes: 17
Views: 16806
Reputation: 137408
Most of the time, the lowest-order bit is called bit 0.
However, it really depends on the context you ask it in. I have worked on two different (interconnected) systems, when one's documentation called it bit 1, and the other's called it bit 0. Talk about confusing! The important thing is to always qualify something if you document it.
Typically, this is called "-indexed". So if the lowest-order bit is called "bit zero" then the bitfield is "zero-indexed."
Personally, I always refer to the lowest-order bit as bit zero. With this convention, you can shift a 1 n
places, to turn on the n
th bit:
x = 1<<0; 00000001b (bit 0 is on)
x = 1<<4; 00010000b (bit 4 is on)
Upvotes: 17
Reputation: 308206
If you go simply by powers of two, 2**0 is 1. It makes more sense to number the bits starting at 0.
Upvotes: 9