Cupid’s little helper has set up a program that treats enumerations as bit fields, in essence turning LoveMeter into a collection of flags. This actually works fairly well in Synergy, as the bitwise AND and bitwise OR operators (& and |, respectively) are completely supported by enumerations in Synergy. This means that comparing, analyzing, or setting flags is not only a snap, but also easy to read.

The program itself is relatively simple, though the usage of an enumeration field as a set of flags may be unfamiliar to some. Since setting and analyzing the flag are really the only points of failure, those are the lines you’re probably concentrating on, so let’s take a look at what they’re doing.

At the very start of the program, the value of suitorStatus is set to zero (LoveMeter.SheLovesHimNot). So far, so good. The next three questions will only change the value of suitorStatus if the answer to the question is “Yes,” so assuming our would-be romantic is doing everything right, then the values of suitorStatus will change as follows:

          1) suitorStatus = suitorStatus | LoveMeter.SentFlowers
              - or -
              suitorStatus = 000 | 100 = 100

          2) suitorStatus = suitorStatus | LoveMeter.MadeReservation
              - or -
              suitorStatus = 100 | 010 = 110

          3) suitorStatus = suitorStatus | LoveMeter.GaveHerACard
              - or -
              suitorStatus = 110 | 001 = 111

The fourth and final question could throw a wrinkle into the mix, because it tries to unset the SentFlowers flag if the “Lady In Waiting” will actually be the “Lady Waiting Forever.” However, since our sample suitor has been careful to cover all of his bases, that line of code is never encountered and we drop through to the comparison:

              if (suitorStatus & LoveMeter.SheLovesHim) != LoveMeter.SheLovesHim
               - or -
              if (111 & 111) != 111
               - or -
              if (7) != 7

Clearly, doing everything right allows the program to continue executing without prematurely looking for the next suitor, and Cupid gets to find the lucky girl and commence with a little target practice. Those of you who answered (b) are out of luck.

But what if the unthinkable happened, and – though completely unheard of – there was problem down at the Post Office, and the flowers didn’t make it to the right address? This time, the answer to the final question would be “No,” and the suitorStatus value should change like so:

          4) suitorStatus = suitorStatus .bxor. LoveMeter.SentFlowers
              - or -
              suitorStatus = 111 .bxor. 100 = 011

Since our final comparison

              if (suitorStatus & LoveMeter.SheLovesHim) != LoveMeter.SheLovesHim
              - or -
              (011 & 111) != 111
              - or -
              (6) != 7

proves true, the program would loop to find the next guy, rather than wasting Cupid’s time and energy tracking down a maiden who’s teary-eyed, shunned, and as luckless as those who answered (c).

As it turns out, those who answered (d) aren’t feeling any love, either. Unfortunately, the bitwise Exclusive OR (.bxor.) is not currently supported by Synergy enumerations, and thus answer (a) is correct, since any attempts to build this program will fail with a compiler error.

But if only bitwise AND and bitwise OR are supported, how then would one go about unsetting a flag in an enumeration used as a bit field? Though a little ugly, there’s actually a fairly easy workaround. Instead of using the .bxor. operator on the enumeration to toggle the bit, one can use the bitwise NOT (~) operator on the base integer – or i4 – data type beneath the enumeration. Rather than using:

suitorStatus = suitorStatus .bxor. LoveMeter.SentFlowers

the same result can be accomplished with:

suitorStatus = suitorStatus & ((LoveMeter)(~(int)LoveMeter.SentFlowers))

Because all the bitwise operations work on integers, this “double cast” – first from an enumeration to an integer, then back again to an enumeration – works around the limitation and lets the code compile (and run!) without a hitch.

Once the basic mechanics and syntax are understood, using enumerations as bit fields become an easy-to-read and space-saving alternative to using long series of D1 values to track flag values. Synergy programmers can pack eight flags into a single byte, or 32 flags into an integer/i4 value, which also make enumerations a great way to expand the number of flags available in a record or structure without the need to resize databases every time a new flag needs to be added.