# Nicer way to test that exactly one flag is set

I have a package that takes a long list of boolean flags (currently about 10) in its options, and I want to test if exactly one of these is set to true - something like an XOR for 10+ booleans.

The only way I can think of is to use the ifthen package and construct a gigantic statement combining \and and \or to test this, but it quickly gets real ugly, and will be incredibly hard to maintain.

Is there a better way to do this?

• If one and only one of the flags should be set to true, then you probably shouldn't be using 10 booleans, but a key that can take 10 values... – Willie Wong Nov 19 '10 at 16:35
• That's not an xor. An xor for a bunch of booleans tests whether an odd number of the booleans is true. – Harald Hanche-Olsen Nov 19 '10 at 16:57
• @Tomas: So you want to test if exactly one is true, right? Then it might be worth rewording the question. – Hendrik Vogt Nov 19 '10 at 17:11
• Concatenate the booleans into a 10-digit binary number and then check if it is a power of 2. Easy. – Seamus Nov 19 '10 at 21:54
• @Tomas: I've slightly reworded the body of your question; OK? – Hendrik Vogt Nov 20 '10 at 9:24

## 2 Answers

You could create a counter, add one for each boolean that is true, then test if the counter has the value 1.

The question does not provide adequate information to provide an exact routine and as Willie Wong commented, it maybe trying to solve the problem with the wrong datastructure.

One approach is to place the booleans in a variable consisting of zeroes and ones. If anyone of the booleans is true then it will then be a number that is greater than zero.

\def\OneTrue#1{\ifnum#1<1 0\fi\number#1}

\OneTrue{1000101010}


One limitation of the macro above is that if the first boolean is 1 then the string can only be ten digits long, otherwise the number will be too big. Depending on the number of booleans one could extend the macro and split it into two to avoid this limitation and parse the two arguments.

What one does with it afterwards, it depends. One can actually use the first boolean that is true to trigger another action. In such a case we would have re-discovered the switch datastructure which is catered by ifcase in TeX!

• I'm not sure what you're trying to do with your \OneTrue macro. Do you want to test if at least one of the booleans is true? That's not what the OP wants. – Hendrik Vogt Nov 20 '10 at 9:28
• @Hendrik that is exactly what he asked quoting `...I want to test if exactly one of these is set to true'. Have I missed something? – Yiannis Lazarides Nov 20 '10 at 10:20
• compare "at least one" with "exactly one". – Hendrik Vogt Nov 20 '10 at 17:48
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.489213764667511, "perplexity": 574.8868791819837}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 5, "enable": false}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-25/segments/1623487608702.10/warc/CC-MAIN-20210613100830-20210613130830-00017.warc.gz"}