8

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.

bitmaker
  • 137
  • 1
  • 11

4 Answers4

15

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.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
5

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. :)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

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.

Pixelchemist
  • 24,090
  • 7
  • 47
  • 71
  • @Caleth If that's confusing, that's because it kind of is, though [there may be _some_ logic to it](https://stackoverflow.com/a/4685115/560648) – Lightness Races in Orbit Nov 09 '18 at 17:11
  • I consider bitwise & and | having lower precedence a design weakness of the language. Origin of is historical, though, as && and || simply have been added later, when precedence was already fixed (and not changed any more for compatibility reasons). – Aconcagua Nov 09 '18 at 17:20
  • Hm, not *really* an answer to the question, is it? Even though the information given is priceless(!) for a beginner... – Aconcagua Nov 09 '18 at 17:23
  • @LightnessRacesinOrbit and this is why I have a pathological disgust of unparenthesised mixed operators – Caleth Nov 09 '18 at 17:30
  • @Aconcagua My link suggests some vague rationale though I'm not wholly convinced by it – Lightness Races in Orbit Nov 09 '18 at 17:39
0

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.

palotasb
  • 4,108
  • 3
  • 24
  • 32
  • "the opposite" is ambiguous. It's true when *any* bits *not in* `ET_TMS` are set in `m_SectorSwitchData.SectorType`. – Caleth Nov 09 '18 at 17:03