Designing a Turing Machine for the 2’s Complement of a Binary String

Designing a Turing Machine for the 2’s Complement of a Binary String

Verified Sources
Sep 11, 2026

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 w{0,1}+w \in \{0,1\}^+, the machine produces:

TC(w)=(flip(w)+1)mod2w\operatorname{TC}(w) = (\operatorname{flip}(w) + 1) \bmod 2^{|w|}

The operation is commonly computed by either:

  1. Complementing every bit and adding 11.
  2. Scanning from right to left, preserving the first 11 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 11 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 202^0"}, most significant bit, and transducer.

Footnotes

  1. 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.

Turing Machine for 2’s Complement

1. Mathematical basis

Let the input be an nn-bit binary string:

w=bn1bn2b1b0w = b_{n-1}b_{n-2}\cdots b_1b_0

Its bitwise complement is:

w=bn1bn2b1b0\overline{w} = \overline{b_{n-1}}\overline{b_{n-2}}\cdots\overline{b_1}\overline{b_0}

The 2’s complement is:

TC(w)=w+1(mod2n)\operatorname{TC}(w) = \overline{w} + 1 \pmod{2^n}

An equivalent right-to-left rule is:

  • Preserve all zeros to the right of the rightmost 11.
  • Preserve the rightmost 11.
  • Flip every bit to the left of that 11.2

For example:

001011001101010000101100 \longrightarrow 11010100

The rightmost 11 is the sixth bit from the left. The suffix 100100 is preserved, while the prefix 0010100101 is complemented to 1101011010.

This equivalence follows because adding 11 to a binary number changes every trailing 11 into 00 until the first 00 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 11 unchanged and complementing the remaining prefix.

Footnotes

  1. The Two's Complement - Explains the right-to-left method: preserve the rightmost 1 and complement bits to its left. 2

  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:

M=(Q,Σ,Γ,δ,q0,qhalt)M = (Q,\Sigma,\Gamma,\delta,q_0,q_{\text{halt}})

where:

ComponentDefinition
QQ{q0,qR,qZ,qC,qhalt}\{q_0,q_R,q_Z,q_C,q_{\text{halt}}\}
Σ\Sigma{0,1}\{0,1\}
Γ\Gamma{0,1,\blank}\{0,1,\blank\}
q0q_0Initial state
qRq_RState that moves right to the input end
qZq_ZState that skips trailing zeros
qCq_CState that complements the prefix
qhaltq_{\text{halt}}Halting state
\blank\blankBlank tape symbol

The transition function has the form:

δ(q,a)=(q,b,D)\delta(q,a) = (q',b,D)

where qq is the current state, aa is the scanned symbol, qq' is the next state, bb is the symbol written, and D{L,R,S}D \in \{L,R,S\} 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

StatePurpose
q0q_0Begin by moving right across the input
qRq_RMove right until the first blank after the input
qZq_ZMove left across trailing zeros
qCq_CComplement each bit to the left
qhaltq_{\text{halt}}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

  1. 1
    Step 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.

  2. 2
    Step 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.

  3. 3
    Step 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.

  4. 4
    Step 4

    For every remaining input bit, write 1 when reading 0 and write 0 when reading 1. Continue moving left.

  5. 5
    Step 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 SS for “stay,” although the construction only needs left and right movements.

Current stateReadWriteMoveNext stateExplanation
q0q_00000RRqRq_RBegin scanning right
q0q_01111RRqRq_RBegin scanning right
qRq_R0000RRqRq_RContinue to the end
qRq_R1111RRqRq_RContinue to the end
qRq_R\blank\blank\blank\blankLLqZq_ZMove to the final input bit
qZq_Z0000LLqZq_ZPreserve trailing zero
qZq_Z1111LLqCq_CPreserve rightmost one
qCq_C0011LLqCq_CComplement zero
qCq_C1100LLqCq_CComplement one
qCq_C\blank\blank\blank\blankRRqhaltq_{\text{halt}}Left boundary reached

In compact notation:

δ(q0,0)=(qR,0,R)δ(q0,1)=(qR,1,R)δ(qR,0)=(qR,0,R)δ(qR,1)=(qR,1,R)δ(qR,\blank)=(qZ,\blank,L)δ(qZ,0)=(qZ,0,L)δ(qZ,1)=(qC,1,L)δ(qC,0)=(qC,1,L)δ(qC,1)=(qC,0,L)δ(qC,\blank)=(qhalt,\blank,R)\begin{aligned} \delta(q_0,0)&=(q_R,0,R)\\ \delta(q_0,1)&=(q_R,1,R)\\ \delta(q_R,0)&=(q_R,0,R)\\ \delta(q_R,1)&=(q_R,1,R)\\ \delta(q_R,\blank)&=(q_Z,\blank,L)\\ \delta(q_Z,0)&=(q_Z,0,L)\\ \delta(q_Z,1)&=(q_C,1,L)\\ \delta(q_C,0)&=(q_C,1,L)\\ \delta(q_C,1)&=(q_C,0,L)\\ \delta(q_C,\blank)&=(q_{\text{halt}},\blank,R) \end{aligned}

5. State diagram

The transition labels use the notation:

read/write,move\text{read}/\text{write},\text{move}

For example, 0/1,L0/1,L means “read 00, write 11, and move left.”

6. Complete execution example

Consider the input:

w=00101100w = 00101100

Its ordinary 2’s-complement calculation is:

00101100invert11010011+11101010000101100 \overset{\text{invert}}{\longrightarrow} 11010011 \overset{+1}{\longrightarrow} 11010100

The Turing machine obtains the same result without explicitly storing a carry.

Tape-level execution

PhaseTape contentsHead positionState
Initial00101100\underline{0}0101100First bitq0q_0
Move right001011000010110\underline{0}Last bitqRq_R
Skip zero00101100001011\underline{0}0Moving leftqZq_Z
Skip zero0010110000101\underline{1}00Rightmost 11qZq_Z
Preserve 11001011000010\underline{1}100Prefix beginsqCq_C
Complement 101 \to 0001001000010\underline{0}100Moving leftqCq_C
Complement 010 \to 100110100001\underline{1}0100Moving leftqCq_C
Complement 101 \to 00001010000\underline{0}10100Moving leftqCq_C
Complement 010 \to 1010101000\underline{1}010100Moving leftqCq_C
Complement 010 \to 111010100\underline{1}1010100Leftmost bitqCq_C
Halt1101010011010100Outside inputqhaltq_{\text{halt}}

Therefore:

0010110011010100\boxed{00101100 \longmapsto 11010100}

7. Additional examples

InputRightmost 11 ruleOutput
0000010100000101Preserve 101101, complement 00000000001111101111111011
0010100000101000Preserve 10001000, complement 001000101101100011011000
1111000011110000Preserve 1000010000, complement 1111110001000000010000
0000000100000001Preserve final 11, complement all preceding zeros1111111111111111
1111111111111111Preserve final 11, complement the preceding seven ones0000000100000001

For example:

000001011111101100000101 \longrightarrow 11111011

The standard method confirms this:

00000101=11111010\overline{00000101}=11111010

and:

11111010+1=1111101111111010+1=11111011

The machine preserves the suffix 101101 and flips the prefix 0000000000, 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 11 exists. If the input is:

00000000

the machine reaches the left blank while still in qZq_Z. Since:

0000+1=1111+1=10000\overline{0000}+1 = 1111+1 = 10000

and the fixed-width result discards the overflow bit, the 4-bit result is:

00000000

To support this case, add:

δ(qZ,\blank)=(qhalt,\blank,R)\delta(q_Z,\blank)=(q_{\text{halt}},\blank,R)

This transition treats the all-zero input as a special fixed-width modulo case.

A complete machine therefore includes:

Current stateReadWriteMoveNext state
qZq_Z\blank\blank\blank\blankRRqhaltq_{\text{halt}}

9. Correctness argument

Let the input be divided as:

w=p1zw = p1z

where:

  • pp is the prefix to the left of the rightmost 11,
  • the displayed 11 is the rightmost 11,
  • zz is the suffix consisting only of zeros.

The machine outputs:

p1z\overline{p}1z

Now consider the ordinary two-step definition. Since w=p1zw=p1z and zz contains kk zeros:

  1. Complementing all bits produces p01k\overline{p}0 1^k.
  2. Adding one changes the trailing 1k1^k into 0k0^k and changes the preceding 00 into 11.

Therefore:

p01k+1=p10k\overline{p}0 1^k + 1 = \overline{p}1 0^k

Since z=0kz=0^k:

TC(w)=p1z\operatorname{TC}(w)=\overline{p}1z

This is exactly the output produced by the machine. Hence the machine is correct for every nonempty binary input containing at least one 11; with the additional blank transition in qZq_Z, it is also correct for the all-zero input.

How to verify a proposed transition table

  1. 1
    Step 1

    Verify that q0 and qR preserve every input bit and that qR stops at the first blank after the input.

  2. 2
    Step 2

    Verify that qZ moves left over every 0 without changing it.

  3. 3
    Step 3

    Verify that qZ preserves the first 1 encountered. This is the point where the implicit addition of one terminates.

  4. 4
    Step 4

    Verify that qC maps 0 to 1 and 1 to 0 while moving left.

  5. 5
    Step 5

    Verify that qC halts when it encounters the blank immediately left of the input.

  6. 6
    Step 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 nn:

  • The initial rightward scan takes nn moves.
  • The leftward scan takes at most n+1n+1 moves.
  • The total running time is:
T(n)=O(n)T(n)=O(n)

The machine uses the original input tape and a constant number of states, so its additional finite control is O(1)O(1). If tape cells visited are counted as space, it uses O(n)O(n) 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

1 / 6
Question · Term

What is the fixed-width formula for 2’s complement?

Click to reveal
Answer · Definition

TC(w)=(flip(w)+1)mod2w\operatorname{TC}(w)=(\operatorname{flip}(w)+1)\bmod 2^{|w|}.

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

Question 1 of 4
Q1Single choice

What is the 2’s complement of the 8-bit string 0010110000101100?