Question

Is there a rough consensus if the bitmask 0x01 is properly said to have the "zeroth" bit set, or the "first" bit set?

If there isn't rough consensus that there's a generally right answer, is there at least rough consensus that there is a contextually right answer (e.g. that how the code is semantically using the bits determines the answer)?

Background

To be clear, I am generally familiar with low-level coding, and am thus comfortable with zero-indexing, but being a human raised in normal human cultures, I am comfortable with one-indexing as well. I am familiar with arguments for both sides. I also read every other question+answers that were suggested to me as I wrote this which seemed to deal with this sort of issue, but none of them were really immediately applicable to bits.

See, if we were talking about arrays, or sequences, or whatever else, I'd find it fairly easy to decide - if dealing with a sufficiently low-level language or platform (where indexes map very directly to offsets or have other convenient properties if zero-indexed), or with people who have zero-indexing ingrained I'd pick that, when dealing with some higher-level abstractions where I expect more "natural" human counting habits, I'd pick the other.

But bits... bits are in this perverse middle ground. Bit are both fundamentally abstract (digits of a number in binary: what's least-significant digit? I'd pick "first" over "zeroth", because this is counting, not indexing) but also highly relevant to extremely low-level work, where the zero-indexing is natural and useful (want just the nth bit? Shift 1 by n).

Motivation

In practice, this came up because I can't decide between these two C code snippets:

/* Bits are one-indexed - even in C, this feels more natural */
#define UCHAR_NTH_BIT_m(n) (unsigned char )(1 << ((n) - 1))
/* Bits are zero-indexed - this feels "purer" in some ways */
#define UCHAR_NTH_BIT_m(n) (unsigned char )(1 << (n))
/* NOTE: undefined behavior if (n < 1) or (n < 0), respectively. */
• As an aside, I'm personally moved by Dijkstra's argument that we should just always count from 0. But his logic means everything should be numbered starting with zero, which definitely is not the general consensus (see questions on here about line numbers, etc). – mtraceur Apr 8 '16 at 8:09
• Bits aren't given numeric positions. Instead we speak of the least significant bit (LSB) and the most significant bit. – Kilian Foth Apr 8 '16 at 8:12
• @KilianFoth: +1 for the more precise verbiage. But let's say the macro was instead called UCHAR_NTH_SIGNIFICANT_BIT_m. Or we just want to discuss not just the two bounding bits, but bits in between? Is 0b0100 the third-least-significant bit? I am inclined to say so, in which case, "least significant bit" is logically shorthand for first-least-significant bit. Would you agree? If so, feel free to post an answer accordingly. I'd +1 it and if no one provides evidence of consensus or better arguments, I'd accept it. – mtraceur Apr 8 '16 at 8:21
• I'd use "bit n" with zero-based indexing instead of "nth bit". – CodesInChaos Apr 8 '16 at 9:07
• One could make the argument that we are talking about a positional number system where digit's value is the digit multiplied by the value of its place (it's distance from the radix point). Where place values are the number of the base raised to the nth power starting with n**0 that 0 would be a good value to 'name' the bit at this position. – thorsten müller Apr 8 '16 at 9:08

Is there a rough consensus if the bitmask 0x01 is properly said to have the "zeroth" bit set, or the "first" bit set?

Your question wouldn't have passed an honest pollsters(oxymoron?) sniff test because it was leading. Of course if you had your question might not have been around long. Try this,

Q: Which bit is on in this, 0x01, assuming little endian?

IMHO you would have received answers that said either / or , bit zero or lsb. It is highly unlikely that any 'coder'("I am not an animal") would have said bit one.

Is 2 to the power of 0 = 1 or is 2 to the power of 1 = 1? Humans imply zero offsets without thought, e.g How old are you? How far is it from your house to work?

My specific answer to this,

#define UCHAR_NTH_BIT_m(n) (unsigned char )(1 << ((n) - 1))

is Please don't. because no human will be looking at it, only 'coders'.

• Despite finding +1-able value in all answers, and that all answers together generally seem to answer the main question in roughly the same vein, I am ultimately accepting this one, because it does two additional things: it answers the specific example case as well as the general question itself in a way that I think is valuable, and it points out the leading nature of my question, which I also think has some value. – mtraceur Apr 10 '16 at 7:02

I would say that 0x01 has "bit 0" set, since it has the bit that corresponds to 20 set. I have a hard time motivating a counting schedule for bits that doesn't preserve that "bit N" is the bit that corresponds to the value 2N.

Bits aren't given numeric positions. Instead we speak of the least significant bit (LSB) and the most significant bit.

Unfortunately, in general things aren't that simple. The generalization of the LSB is the "lower 8 bits" or the "lower 4 bits", and you might speak of e.g. the 3rd-lowest bit. Insofar, terminology is definitely 1-based. But there is also the vexed issue of Endianness, which means that on many systems, the 9th-lowest bit of an integer is not actually located in 9th position no matter which way you scan the memory cells. Perhaps that is one reason why individual bits in a 32-bit number are rarely given numbers.