# Decimal to Binary Converter: an electronics-based recursive approach.

The binary equivalent of a decimal number can be found creating a binary counter that counts from 0 to the decimal number.
The least significant bit of the binary number (counter) always toggles (from 0 to 1 and from 1 to 0), but when one bit changes from 1 to 0, the next bit also toggles and this effect repeats recursively until the counter reaches the decimal number.
The binary number is shown as a list of bits. The first item of the list is the least significant bit, and the last item of the list is the most significant bit.

That's a very interesting approach. I've seen binary counters before, but not as an algorithm to convert an input number to binary.

You have pushed one of my buttons. (My colleagues will tell you how fixated I am on this point.) You are not converting from decimal to binary. You are converting from number to binary numeral. Snap! (like all programming languages) normally converts from decimal numeral to number on input, and from number to decimal numeral on output. (Thinking about this is complicated by the fact that, of course, computers can't directly represent numbers, and today's computers all use binary numerals internally as the number representation. But the typical algorithms, which are notoriously hard for students, are much easier to understand if you'll humor me by pretending, for the moment, that computers really do store numbers internally, as a bag of n marbles, for example. Or think about the old days, with business computers that represented numbers as decimal numerals.)

So, here's my favorite to-binary algorithm:

What I want to point out about this program is that the operations performed on the input number are arithmetic: <. /, floor, mod. But the operation performed on the binary representation, a numeral, is join, a string operation. Here's converting from a binary numeral to a number:

Here the input is a numeral, and the operations applied to it are length of, all but last letter of, and last letter of. The return value, a number, is computed using × and +.

The point is somewhat obscured by the way Snap! treats numbers as strings of digits. The point would be even clearer if, instead of binary, I converted to and from hexadecimal, since hexadecimal numerals are not strings of (decimal) digits.

If you think of it as decimal-to-binary and binary-to-decimal, there's really no good reason not to use the same algorithm for both directions! This is what makes it hard for kids, imho. If instead you think in terms of number-to-numeral and numeral-to-number, the reason for the asymmetry is clear, and by taking the base as a second input, the same procedures can be generalized to convert numbers to and from numerals in any base, including decimal.

As I said, nothing to do with your project, just your vocabulary. There's no such thing as a "binary number" or "decimal number." And what seems like meaningless fetishism actually helps in talking about conversion.

Thank you very much for your note. I understand and fully agree with you (I guess I also share your fixation with correctness).
BTW, I'm glad you pointed me to the "Words, sentences" library (really useful).

It's part of our effort to be the true successor to Logo.