Designing a Turing Machine for the 2’s Complement of a Binary String
This section develops a single-tape deterministic Turing machine that transforms a binary string into its fixed-width 2’s complement.
For an input string , the machine produces:
The operation is commonly computed by either:
- Complementing every bit and adding .
- Scanning from right to left, preserving the first encountered and complementing every bit to its left.2
The second method is especially suitable for a Turing machine because the carry from “add one” is handled implicitly: all trailing zeros remain unchanged, the rightmost remains unchanged, and only the more significant bits are flipped.
Key terms include 2’s complement, [least significant bit]{def="The rightmost bit, having place value "}, most significant bit, and transducer.
Footnotes
-
The Two's Complement - Explains the right-to-left method: preserve the rightmost 1 and complement bits to its left. ↩
-
Two's Complement - Provides the invert-and-add-one definition and binary examples. ↩
Turing Machine for 2’s Complement
1. Mathematical basis
Let the input be an -bit binary string:
Its bitwise complement is:
The 2’s complement is:
An equivalent right-to-left rule is:
- Preserve all zeros to the right of the rightmost .
- Preserve the rightmost .
- Flip every bit to the left of that .2
For example:
The rightmost is the sixth bit from the left. The suffix is preserved, while the prefix is complemented to .
This equivalence follows because adding to a binary number changes every trailing into until the first is reached. When starting from the original string and working from the right, the same effect is achieved by leaving the suffix through the rightmost unchanged and complementing the remaining prefix.
Footnotes
-
The Two's Complement - Explains the right-to-left method: preserve the rightmost 1 and complement bits to its left. ↩ ↩2
-
Two's Complement - Provides the invert-and-add-one definition and binary examples. ↩
Core design insight
Do not explicitly perform bitwise inversion followed by binary addition. Scan from the least significant end, preserve the first 1, and complement all remaining bits.
2. Machine specification
We use a deterministic single-tape machine:
where:
| Component | Definition |
|---|---|
| Initial state | |
| State that moves right to the input end | |
| State that skips trailing zeros | |
| State that complements the prefix | |
| Halting state | |
| Blank tape symbol |
The transition function has the form:
where is the current state, is the scanned symbol, is the next state, is the symbol written, and is the head movement.
The input is written contiguously on the tape, with blanks elsewhere. The machine overwrites the input in place and leaves the result with the same length.
3. State meanings
| State | Purpose |
|---|---|
| Begin by moving right across the input | |
| Move right until the first blank after the input | |
| Move left across trailing zeros | |
| Complement each bit to the left | |
| Stop after reaching the left boundary |
The machine assumes a nonempty binary input. Since the input contains at least one symbol, the leftmost input position can be detected when the machine encounters a blank while moving left.
Algorithm for the Turing machine
- 1Step 1
Starting at the leftmost input bit, move right over every 0 and 1. When the first blank is found, move one cell left to the least significant bit.
- 2Step 2
While scanning left, leave every 0 unchanged. These zeros are the suffix that remains unchanged during the right-to-left 2’s-complement procedure.
- 3Step 3
When the first 1 is found while moving left, leave it unchanged and move one cell left. The carry generated by adding one has now been absorbed.
- 4Step 4
For every remaining input bit, write 1 when reading 0 and write 0 when reading 1. Continue moving left.
- 5Step 5
When a blank is encountered to the left of the input, enter the halting state. The tape now contains the 2’s complement.
4. Transition table
The transition table below uses for “stay,” although the construction only needs left and right movements.
| Current state | Read | Write | Move | Next state | Explanation |
|---|---|---|---|---|---|
| Begin scanning right | |||||
| Begin scanning right | |||||
| Continue to the end | |||||
| Continue to the end | |||||
| Move to the final input bit | |||||
| Preserve trailing zero | |||||
| Preserve rightmost one | |||||
| Complement zero | |||||
| Complement one | |||||
| Left boundary reached |
In compact notation:
5. State diagram
The transition labels use the notation:
For example, means “read , write , and move left.”
6. Complete execution example
Consider the input:
Its ordinary 2’s-complement calculation is:
The Turing machine obtains the same result without explicitly storing a carry.
Tape-level execution
| Phase | Tape contents | Head position | State |
|---|---|---|---|
| Initial | First bit | ||
| Move right | Last bit | ||
| Skip zero | Moving left | ||
| Skip zero | Rightmost | ||
| Preserve | Prefix begins | ||
| Complement | Moving left | ||
| Complement | Moving left | ||
| Complement | Moving left | ||
| Complement | Moving left | ||
| Complement | Leftmost bit | ||
| Halt | Outside input |
Therefore:
7. Additional examples
| Input | Rightmost rule | Output |
|---|---|---|
| Preserve , complement | ||
| Preserve , complement | ||
| Preserve , complement | ||
| Preserve final , complement all preceding zeros | ||
| Preserve final , complement the preceding seven ones |
For example:
The standard method confirms this:
and:
The machine preserves the suffix and flips the prefix , producing the same result.
Important edge case: the all-zero string
For input 000...000, no 1 is encountered. A complete implementation should add a transition in qZ for blank that moves to qhalt, leaving the all-zero string unchanged. This represents 0 modulo 2^n.
8. Handling the all-zero input
The transition table above assumes that a exists. If the input is:
the machine reaches the left blank while still in . Since:
and the fixed-width result discards the overflow bit, the 4-bit result is:
To support this case, add:
This transition treats the all-zero input as a special fixed-width modulo case.
A complete machine therefore includes:
| Current state | Read | Write | Move | Next state |
|---|---|---|---|---|
9. Correctness argument
Let the input be divided as:
where:
- is the prefix to the left of the rightmost ,
- the displayed is the rightmost ,
- is the suffix consisting only of zeros.
The machine outputs:
Now consider the ordinary two-step definition. Since and contains zeros:
- Complementing all bits produces .
- Adding one changes the trailing into and changes the preceding into .
Therefore:
Since :
This is exactly the output produced by the machine. Hence the machine is correct for every nonempty binary input containing at least one ; with the additional blank transition in , it is also correct for the all-zero input.
How to verify a proposed transition table
- 1Step 1
Verify that q0 and qR preserve every input bit and that qR stops at the first blank after the input.
- 2Step 2
Verify that qZ moves left over every 0 without changing it.
- 3Step 3
Verify that qZ preserves the first 1 encountered. This is the point where the implicit addition of one terminates.
- 4Step 4
Verify that qC maps 0 to 1 and 1 to 0 while moving left.
- 5Step 5
Verify that qC halts when it encounters the blank immediately left of the input.
- 6Step 6
Test 000...000, 000...001, 111...111, and inputs whose rightmost 1 is near the middle.
10. Complexity analysis
For an input of length :
- The initial rightward scan takes moves.
- The leftward scan takes at most moves.
- The total running time is:
The machine uses the original input tape and a constant number of states, so its additional finite control is . If tape cells visited are counted as space, it uses tape cells, which are already occupied by the input and its boundary blanks.
The construction is therefore a linear-time, in-place Turing-machine transduction.
Approximate machine work by input length
The rightward and leftward scans together require linear work.
Common questions and edge cases
2’s Complement Turing Machine Review
Exam-ready construction
Write the machine in three conceptual phases: move right to the end, move left over zeros and preserve the first 1, then move left while flipping every remaining bit.
Knowledge Check
What is the 2’s complement of the 8-bit string ?
Explore Related Topics
When Can a Language Be Accepted by a Turing Machine?
Designing a Four-Input Logic Circuit with an Equality Condition
Converting the Regular Expression $(a+b)^*ab$ into an NFA
The course shows how to turn the regular expression —the set of all strings over that end with “ab”—into an NFA, first via Thompson’s systematic ε‑construction and then with a minimal three‑state NFA.
- Thompson’s construction builds ε‑transitions for symbols , , their union, the Kleene star, and the final concatenations, yielding a 12‑state ε‑NFA.
- A compact direct NFA uses only three states: loops on , branches on to , and moves on to accepting .
- The compact NFA’s transition table illustrates nondeterministic moves (e.g., ) and accepts exactly the strings ending in “ab”.
- State‑complexity comparison: Thompson’s method needs many states and ε‑moves, while the direct NFA is far smaller and easier to simulate.