ARITHMETIC WORKSHEETS

Checking duplicate flags before using addition

Adding flag values works only when each flag occupies its own bit. A repeated flag adds arithmetically and carries into the next bit.

What a flag value is

A bit flag is a power of two assigned to one option: 1, 2, 4, 8 and so on. Each value occupies one position in a binary number, so a set of selected options can be stored in a single integer. Flags 1 and 2 give binary 01 and 10, and selecting both produces 11, which is 3. This works because writing a number in binary is unique, and distinct powers of two never interfere. The trouble starts when the same flag is supplied twice. Addition treats the second copy as a second quantity, not as a repeat of the same selection, and the result is a different number from the flag set the sender intended.

Adding disjoint flags

The basic calculator returns 3 for 1 plus 2. The Integer Base Converter, with value 3 in base 10 and target base 2, returns 11, so both low positions are set. The positions do not overlap, which is the condition that makes addition safe. Adding a third flag keeps the pattern: 4 plus 3 gives 7, binary 111. A system with eight distinct flags, 1 up to 128, produces a total of 255 when all are selected, which is 2 to the 8 minus 1. Because every sum below 256 corresponds to exactly one set of those flags, a receiver can decode the integer back into options by reading its bits.

Adding the same flag twice

Now add flag 1 to itself. The basic calculator returns 2 for 1 plus 1. In binary, 2 is 10, which is flag 2, a different option entirely. The addition carried out of position zero into position one. A receiving system would read that result as flag 2 selected and flag 1 not selected, which is the opposite of the intended selection. The sum does not remember that two copies were supplied. A duplicate silently turns into another option, and no error appears. Three copies of flag 1 add to 3, which then reads as flags 1 and 2 together, so the pattern of errors depends on how many repeats occur.

Union, not sum, is the operation for sets

A set that contains flag 1 twice still selects only one thing. The operation that represents this is the bitwise OR, which sets a bit when either input has it set. Computing 1 OR 1 gives 1, and 1 OR 2 gives 3. Addition and OR agree only when the inputs share no bits. The basic calculator on this site offers addition, subtraction, multiplication and division, so it can confirm the arithmetic sums used here, but it does not perform bitwise OR. When you use a programming language, choose the bitwise operator for combining flags and keep the plus sign for counting.

A mixed example with three flags

Take flags 1, 2 and 8, as in options labeled read, write and archive. Adding 1 and 2 gives 3 and adding 8 gives 11. The base converter turns 11 in base 10 into 1011 in base 2, with ones at positions zero, one and three. Each set bit identifies one option. If an input list repeated flag 8, the sum would be 19, which in binary is 10011, setting position four, an option that nobody requested. A decoder reading 19 would report flags 1, 2 and 16. The repeated 8 vanished and an unrelated 16 appeared, the same carry effect as with 1 plus 1 but at a higher position.

Unix permissions as a familiar case

The read, write and execute permission bits on Unix and Linux systems are 4, 2 and 1. A permission digit is the sum of the selected bits: 6 for read and write, 7 for all three, 5 for read and execute. Because each bit is distinct, a mode such as 644 decodes cleanly into owner, group and others. If a script accidentally added 4 twice, the sum 8 would not even be a valid permission digit. The example shows why such integer encodings need the distinct-bit rule: the digit has only three bit positions, and a carry has no valid meaning there.

Deduplicate before summing

When a reconstruction must use addition, deduplicate the list of flags first. Sorting the list and removing equal neighbors guarantees that every power of two appears at most once, after which the sum equals the bitwise union. An alternative is to validate each flag as a power of two and track which positions have been seen, rejecting a second copy with an error message. Either approach turns a silent mistake into a handled one. In spreadsheets, a conditional count such as counting equal entries gives a quick way to find duplicates before the totals are formed. The most reliable design is a single union operation applied directly.

Integer width and the top flag

Flags are stored in integers of fixed width, commonly 8, 16, 32 or 64 bits. A 32-bit field has positions 0 through 31, so the highest flag is 2 to the 31, which is 2,147,483,648, and all 32 flags together sum to 4,294,967,295. In languages that use signed 32-bit integers, the top flag lands on the sign bit and can read as a negative number, so unsigned types or wider integers are common for flag sets. The power calculator confirms 2 to the 31 as 2,147,483,648. When you add flags by hand, check that the largest one fits the declared width. A value above the width overflows or wraps, which is another way for a sum to deviate from the intended set.

What this page does not test

The worksheet checks the arithmetic of disjoint and repeated flags with the basic calculator and the representation of the result with the base converter. The converter works on the digit string and does not know your flag layout, which flags exist, or whether the value fits a field of a given width. It cannot tell you that 3 means read and write in your system. Define the flag table, the width of the integer and the combining operation in the specification you are following, and use these sums only to check the arithmetic of the cases that comply with it. A passing sum shows that the arithmetic is right for the inputs given, and it says nothing about whether those inputs were the correct flags.

Reproduce the checks

Disjoint flag sum

Open the Basic calculator and enter:

First number
1
Operation
add
Second number
2

Expected output:

Result

3

The positions do not overlap.

Repeated flag arithmetic

Open the Basic calculator and enter:

First number
1
Operation
add
Second number
1

Expected output:

Result

2

The sum carries into another position.

Combined disjoint pattern

Open the Integer Base Converter and enter:

Value
3
Source base
10
Target base
2

Expected output:

Value

11

Both distinct low positions are set.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks