I made a block that writes numbers in binary!

I managed to create a block that converts a number into base 2! It took an hour or two to make, and a ton of debugging, but I finally managed to get it to work!

Base 2 Converter! script pic (1)

Here's the code!

Base 2 Converter!

P.S. It works with negative numbers!

Wow that's complicated. I can't even try to read it, because of your single-letter variable names, so I have no idea what the algorithm is supposed to be! I guess a is supposed to be list of bits but that's as far as I got. I'd be happy to read your code if you used good variable names.

Hint: The best, most elegant solution is of the form
untitled script pic
(you fill in the blanks). An algorithm that's a bit less elegant, but I'm told is easier for learners to understand, looks like

(In this algorithm, the first empty slot is really supposed to be empty, but you're supposed to fill in the other ones.)

Okay. I edited it and gave the variables more clear names.

P.S. It seems much longer, and it is, but the only difference is the variable names' lengths.

Ah, thanks! Now I get what you're doing. Don't you see how much clearer that is?

Your last REPEAT UNTIL block is like my second algorithm. Apart from remembering the sign, all the stuff before that loop is because you don't know about the lg function, which takes log₂ of its input. (Log, if you don't know it, is the inverse function to exponentiation. Basically it tells you the largest power of 2 in the number.)

I got an error:

There. Fixed it. The problem was that I misspelled "is num negative?" and wrote "is num negitave?" which are two completely different names.

Oh. That would be really helpful if I knew that before I started programming it. Thanks!

I made one where you can convert a number into any base. (only up to base 16)
Here's the link: Number Converter! by dms_64

Does not work with negitive numbers yet!

Nice! Maybe represent negative numbers with 2's complement?

It works with negative numbers now!