Knack Hack: Binary Addition
Knack (an online database and application platform, and also my employer) is a great tool with any number of practical uses. However, as with any sufficiently powerful system, you can also make it do cool things it was never intended to do. In this article, I will show how I used Knack to make a simple computer that adds two 4-bit binary numbers together.
Architecture
This application consists of two objects representing the two stages of the addition operation: a full adder object which stores input and output values for each bit, and a binary adder object that chains together full adders and contains an action to trigger computation.
Each full adder record has three input bits (A and B, set by the user as inputs, and C(in) input carry bit set by the binary adder’s action) and two output bits (S, the result, and the C(out) output carry bit) each set using conditional field rules corresponding to the full adder’s truth table.
One issue I had in this application in my first attempt was that I only added the positive (Yes) conditions of the truth table to the field rules to try to simplify things — this caused the calculation to work correctly on the first run but not on subsequent runs as results could never be reset to No. After adding the negative conditions as well, the calculation worked every time.
Each bit of the calculation needs to be initialized as a new full adder record, with only A and B needing to be set. After all the bits have been set up, they are assigned to a binary adder object record, which is just four 1–1 connections between binary adder and full adders (and which is the subject of an action link).
Computation
Computation is done in the live app via a detail view of a binary adder object. The detail displays the A, B, and S values for each bit and an action link that trigger the computation. This step is needed in order to propagate the carry bit through each adder, in turn triggering the record rule to set the result S.
And voilà!
Thanks for reading! If you have any thoughts on this piece or want to share weird stuff you’ve built in Knack, hit me up at @Overlapping on Twitter.