CARDIAC › Tips and tricks › Digits
Fold the multiply into the shift
Say you want to reverse a three-digit number, so 123 becomes 321.
You'd probably cut out the three digits one at a time, then use another shift on each to put it in its new place, and add them up.
cell contents notes
10 040 INP 40 cells 10-19: cut out each digit: c, then b, then a
11 140 CLA 40
12 433 SFT 33
13 643 STO 43
14 140 CLA 40
15 423 SFT 23
16 642 STO 42
17 140 CLA 40
18 413 SFT 13
19 641 STO 41
20 143 CLA 43 cells 20-29: then put each in its place and add them up
21 420 SFT 20
22 644 STO 44
23 142 CLA 42
24 410 SFT 10
25 244 ADD 44
26 241 ADD 41
27 644 STO 44
28 544 OUT 44
29 900 HRS 00
40 000
41 000
42 000
43 000
44 000
Step through the natural way in Cardigan
Here's what makes SFT so handy: SFT xy shifts left x places and then right y, and whatever falls off either end is gone. That means one shift can cut a digit out and drop it where it belongs, all at once. From abc, SFT 31 gives you c00, and SFT 13 gives you 00a. Add each piece to a running total as you go. This is how the manual's own Program No. 6 does it.
cell contents notes
10 040 INP 40 cells 10-24: each shift cuts a digit out and puts it in its new place at once
11 140 CLA 40
12 431 SFT 31 c00
13 641 STO 41
14 140 CLA 40
15 423 SFT 23 00b
16 410 SFT 10 0b0
17 241 ADD 41
18 641 STO 41
19 140 CLA 40
20 413 SFT 13 00a
21 241 ADD 41
22 641 STO 41
23 541 OUT 41
24 900 HRS 00
40 000
41 000
Step through the CARDIAC way in Cardigan
Both turn 123 into 321. Cutting and placing separately takes 25 cells and runs 20 instructions; folding them together takes 17 cells and runs 15.
From the tips in
cardiac-code on GitHub. All the tips.