Return True Only When One of the Three Inputs Is True

At the $DAYJOB I was trying to figure out how to have a contract for a function that ensures that only one of the three conditionals needed is true. Given that I had come from an electrical engineering background, my first thought was to write a boolean expression. One goes about this by first constructing a truth table and either forming an expressing with a “Sum of Products” (SOP) or a “Product of Sums” expression from the table.

A B C F
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
1 1 1 0

SOP expression seems to look like the appropriate option here and you get, $$ F = A\bar{B}\bar{C} + \bar{A}B\bar{C} + \bar{A}\bar{B}C $$

I thought that writing it in C++ will look weird given the long descriptive variable names I had and it didn’t convey the intention of the assert right away. On Stack Overflow, I found a more C++ solution to the problem that looked a bit easier to justify which suggested just summing up the conditionals after casting them. After converting them to something nicer with static_cast, I thought this was acceptable.

const bool only_one_has_changed = (static_cast<int>(the_first_conditional) +
                                   static_cast<int>(the_second_conditional) +
                                   static_cast<int>(the_third_conditional) == 1)

In the end after writing this post, I am not sure if the code solution is any better than just translating the SOP expression into a C++ statement.

Have you written a response to this? Let me know the URL