~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Operator Precedence / 2009-08-02
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

I usually don't pay much attention to operator precedence, thinking that it's either obvious, or it gets parentheses. And so recently I got bit in the face by it. I just spent a couple hours trying to find a bug in a set of classes with some nested data buffers and such, until I finally got to this method, which is supposed to extract a bit from an array of unsigned chars:

int DataBuffer::getBit(int index)
{
	int whichByte=index/8;
	int offset=index%8;
	unsigned char theByte=this->get(whichByte);
	unsigned char mask=1<<(7-offset);
	if(theByte&mask==0)return 0;
	return 1;
}
It seems the method was returning 1 at inappropriate times. The hardcore C++ operator precedence nerds will have immediately noticed that I should have placed some parentheses around my "theByte&mask". Good work all you hardcore C++ operator precedence nerds.

--------------------
:::Comments:::