Binary Conversion

Brian's Notes from late July 2016: One of them tried decimal to binary conversion, and ended up bringing me a solution using an integer exponentiation function because he didn't see the hint about going from right to left. Maybe we should make that more prominent? (He'd gotten it working but felt that his solution was too complicated and wanted to know if there was an easier way.) (7.3.3)

PG: Do we love binary enough to keep this? I absolutely love the mathematics here. The hint page does really teach—and could be the basis for a lesson on recursion because it makes clear that one result is based on another—but it seems focused on getting /this task/ done, not on abstracting a general idea of recursion. I think this is YADA (yet another distracting activity).

BH: Number->binary. And merge that hints page back in. (Mary agrees)

Pages or lab needs renaming so they aren't duplicative. --MF, 6/15/20

Brian wants to know, "Where's binary to number?" (inverse function) --MF, 7/6/20

Add specs or the algorithm. --MF, 7/6/20

The block should be renamed TO BINARY. Its input is a number, not a character string. --Brian, 4/22/22

We should discuss this (probably with Paul) before deciding on the change; whatever we decide to do, that change should be made together (you and I together) so that all instances are cleaned up and we aren't left with inconsistencies. --MF, 4/22/22

In this lab, you will extend your work with binary representation from Unit 4 to programming base conversion in Snap!.

On this page, you will explore a more elegant approach to base conversion.

In Unit 4 you learned about the binary representation of a number. In this project, you'll write a reporter decimal->binary that takes a number (which Snap! represents in decimal) as input, and reports a string of zeros and ones that represent the same number in binary:
decimal->binary (9827) reporting 10011001100011

To solve this problem, you'll need to work with integer division into a quotient and remainder. For example, 17 ÷ 5 is 3 with a remainder of 2. Here's how to find the quotient and remainder in Snap! :
(floor) of (17/5), reporting 3 (17)mod(5), reporting 2

The floor of a number is the largest integer less than or equal to that number. For example, the floor of 3.87 is 3. The floor function is in a block with other mathematical functions. In the palette, it looks like this: sqrt You'll need to change the operation in the drop-down menu.

The mod block reports the remainder when the first input is divided by the second. You learned about mod on Unit 2 Lab 4 Page 1: The Mod Operator.

From AC: ask students to build the integer division block. BK: did not choose to do this because "floor" is very hard to locate. There are multiple methods to calculate the integer division, but the "floor" method is the most clear constant time method.

Has a hints page. Links to an old page that’s no longer actively used in the curriculum. Why not use the normal hints feature? --Michelle via MF, 4/22/22

  1. Write the decimal->binary block.

    Need a hint?

    The biggest hint is that building the binary string should work from right to left, because the rightmost bit is always the ones column, while the leftmost bit's value changes depending on how many digits the numeral has.

    Need more hints?

    Remember, the input to your block is a number, so you do arithmetic on it; the value reported by your block is a text string, so you need string operators (join) to make it.

    Need a bit more support?

    Try doing these calculations by hand, and look for connections. The connections may help you understand how to write your code.

    Talk with Your Partner
    • Explain why 13 is 1101 in binary.
    • Write 26 in binary. Write 27 in binary. How is each related to writing 13 in binary?
    • What are the quotient and remainder when you divide 27 by 2?
  2. "U8L2-Base-Conversion"Save your work as U8L2-Base-Conversion