Binary subtract one

Example 03

The head reads one cell. Zero stays zero; leading zeros are kept.

Instruction Table

StateReadWriteMoveNext state
Check for 100RightCheck for 1
Check for 111RightFind the end
Check for 1□□StayHalt
Find the end00RightFind the end
Find the end11RightFind the end
Find the end□□LeftBorrow
Borrow01LeftBorrow
Borrow10StayHalt

The outlined rule is the one the machine will use next.

State Machine

0 steps
Binary subtract oneCheck for a one. If the input is zero, halt unchanged. Otherwise find the end, borrow left through trailing zeros, change the first one to zero and halt.0 → 0, Right1 → 1, Right□ → □, Stay0 or 1 → keep, Right□ → □, Left0 → 1, Left1 → 0, StayStartCheckfor 1Findthe endBorrowHalt