What does "&varname == varname" mean?

Viewed 861

I've inherited a C++ project at work, it's an application originally written for Windows XP that presents visual stimuli during psych experiments. I can't figure out this line here that checks for syntax errors in the control file:

else if((m_SectorSwitchData.SectorType &ET_TMS == ET_TMS) & (m_SectorSwitchData.SectorType | ET_TMS != ET_TMS))

I can't find any documentation on what the "&ET_TMS == ET_TMS" means, is it a typo? The Wikipedia page on C++ operators doesn't mention it and Visual Studio doesn't mark it wrong.

4 Answers

This is the bitwise and operation. To make it easier to parse you could add a space and some parentheses to make it easier to read:

(m_SectorSwitchData.SectorType & ET_TMS) == ET_TMS

Do note that this change will actually change the behavior of the code. & has a lower precedence than == so

(m_SectorSwitchData.SectorType & ET_TMS == ET_TMS)

is actually

(m_SectorSwitchData.SectorType & (ET_TMS == ET_TMS))

This is mostly a mistake by the original author and

((m_SectorSwitchData.SectorType & ET_TMS) == ET_TMS)

is most likely what they intended to have. This also applies to the second part of the if statement.

There is a typo, but it's not with the &.

The typo is that a & b == c means a & (b == c) when, at least to me, it looks like the author probably intended (a & b) == c.

Now, don't be misled by the missing space — this is not a reference or an address-of operation or anything like that; it's a bitwise AND with the subsequent conventional whitespace omitted. In the mirror-image condition immediately after it you see a similar condition, except with bitwise OR and the whitespace included.

C++ doesn't really care about whitespace as long as tokens can be identified unambiguously, and it can identify them unambiguously based on what is valid where.

a &b
a & b
a& b
a&b

Given that a and b are already known to be expressions, the above four lines are equivalent.

Of course, if a were a typename, then they would all be declarations (or parts of a declaration) of a reference called b!

Those with sense, though, write the expression variety like this:

a & b

…and the declaration variety like this:

a& b

Some people with no sense write the declaration variety like this:

a &b

… but nobody I know would write the expression variety like that, because it's weird and confusing, as you've discovered. :)

In fact, equality comparison == has higher precedence than bitwise and &.

a & b == b is the same as a & (b == b) but not (a & b) == b.

The line expression (m_SectorSwitchData.SectorType & ET_TMS == ET_TMS) can be parenthesized ((m_SectorSwitchData.SectorType & ET_TMS) == ET_TMS) and it applies bitwise and to m_SectorSwitchData.SectorType. The result will be a number where only those bits of m_SectorSwitchData.SectorType will remain set to 1 that are 1 in the ET_TMS constant. Checking if this is equal to ET_TMS is equivalent to checking that all the bits that are 1 in ET_TMS are also set in m_SectorSwitchData.SectorType. The other half of the if is the opposite.

Related