SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

CARDIAC › Tips and tricks › Digits

Pack three counts into one cell

Say you're keeping score over a season: how many wins, draws and losses, each fewer than ten.

The obvious way gives each count its own cell. Then every result needs questions to find out which count it belongs to, and printing the counts takes three OUTs.

cell  contents  notes
 10      040    INP 40  cells 10-12: read results until a negative card: 2 a win, 1 a draw, 0 a loss
 11      140    CLA 40
 12      329    TAC 29
 13      700    SUB 00  cells 13-16: which one?
 14      325    TAC 25
 15      700    SUB 00
 16      321    TAC 21
 17      141    CLA 41  cells 17-28: count it in its own cell
 18      200    ADD 00
 19      641    STO 41
 20      810    JMP 10
 21      142    CLA 42
 22      200    ADD 00
 23      642    STO 42
 24      810    JMP 10
 25      143    CLA 43
 26      200    ADD 00
 27      643    STO 43
 28      810    JMP 10
 29      541    OUT 41  cells 29-32: print wins, draws and losses
 30      542    OUT 42
 31      543    OUT 43
 32      900    HRS 00
 40      000    the card
 41      000    wins
 42      000    draws
 43      000    losses

Step through the natural way in Cardigan

But a cell has three decimal digits, and each count fits in one. Let the hundreds count the wins, the tens the draws and the units the losses, and put each result on its card as the amount it adds: 100 for a win, 10 for a draw and 1 for a loss. Now there's nothing to sort out, because adding up the cards keeps all three counts, and 213 means two wins, one draw and three losses. You can still pull out any one count when you need it: SFT 13 leaves the hundreds, SFT 23 the tens and SFT 33 the units. Just don't let a count pass 9, or it spills into the next one.

cell  contents  notes
 10      040    INP 40  cells 10-17: a win is 100, a draw 10 and a loss 1, so the sum keeps all three counts
 11      140    CLA 40
 12      316    TAC 16
 13      241    ADD 41
 14      641    STO 41
 15      810    JMP 10
 16      541    OUT 41
 17      900    HRS 00
 40      000    the card
 41      000    wins, draws and losses, one digit each

Step through the CARDIAC way in Cardigan

Three cells

001122334455667788990000101020304050607080910INP11CLA12TAC13SUB14TAC15SUB16TAC17CLA18ADD19STO20JMP21CLA22ADD23STO24JMP25CLA26ADD27STO28JMP29OUT30OUT31OUT32HRS3334353637383940card41W42D43L4445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

CodeThe counts: wins, draws and lossesVariables and constantsCell 00, always 001Free

27 cells, three of them for the counts.

One cell

001122334455667788990000101020304050607080910INP11CLA12TAC13ADD14STO15JMP16OUT17HRS1819202122232425262728293031323334353637383940card41WDL42434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

CodeThe counts, one digit eachVariables and constantsCell 00, always 001Free

10 cells, and one of them holds all three counts.

Both find two wins, one draw and three losses, and the second prints them as 213. Counting in three cells takes 27 cells and runs 67 instructions; counting in one takes 10 cells and runs 41. The same packing puts three yes-or-no flags, or three small numbers, in every cell, which is how a program fits data three times bigger than it would otherwise have room for.

Unix file permissions pack the same way, in octal. The digits in chmod 754 are for the owner, the group and everyone else, and each one is the sum of 4 for read, 2 for write and 1 for execute. C's bitfields and the x86 flags register do the same with bits.

From the tips in cardiac-code on GitHub. All the tips.