diff --git a/data/lessons/authored/arith.place.binary.md b/data/lessons/authored/arith.place.binary.md index 95eae1873..f49e6ea09 100644 --- a/data/lessons/authored/arith.place.binary.md +++ b/data/lessons/authored/arith.place.binary.md @@ -4,16 +4,37 @@ Binary writes a number with only zero and one, using powers of two as place valu ## Powers of two as places -The places in a binary numeral are one, two, four, eight and so on. Each digit says whether that power is present. Reading from the right, the place values double at each step. +The places in a binary numeral are one, two, four, eight, and so on. Each digit says whether that power is present. Reading from the right, the place values double at each step: \(2^0 = 1\), \(2^1 = 2\), \(2^2 = 4\), \(2^3 = 8\), and \(2^4 = 16\). + +**Example:** What is the largest power of two that is at most 21? +Double 1 repeatedly until the next double would exceed 21: \(1, 2, 4, 8, 16, 32\). Since \(16 \le 21\) and \(32 > 21\), the largest power of two is **16**. ## Converting a numeral -Multiply each one bit by its place value and add the results. For example, 1011 has ones in the eight, two and one places, so its value is eleven. The leading one is the largest power of two not exceeding the number. +Multiply each bit by its place value and add the results. Reading from right to left, the exponents increase starting at \(2^0\). + +**Example:** Convert \(1011_2\) to decimal. +Read the bits from right to left: +* \(1 \times 2^0 = 1\) +* \(1 \times 2^1 = 2\) +* \(0 \times 2^2 = 0\) +* \(1 \times 2^3 = 8\) + +Add the results: \(1 + 2 + 0 + 8 = 11\). So \(1011_2 = 11\). ## Counting one bits The number of one bits is the number of powers of two in the expansion. It is also called the popcount. Repeatedly halving the number and adding the remainders counts the same bits. -The three sections form one chain. The first names the object, the second performs the computation and the third reads the result in context. +**Example:** How many 1 bits are in the binary expansion of 13? +Divide by 2 repeatedly and track the remainders: +* \(13 \div 2 = 6\) remainder 1 +* \(6 \div 2 = 3\) remainder 0 +* \(3 \div 2 = 1\) remainder 1 +* \(1 \div 2 = 0\) remainder 1 + +Reading the remainders from bottom to top gives \(1101_2\). There are **3** ones in the expansion. + +--- Practice should be answered without a calculator first. Write the relation, substitute the published values and state the integer result. Then check the result against the defining identity or a second representation before moving to the next question. diff --git a/internal/generator/arithmetic/wave8.go b/internal/generator/arithmetic/wave8.go index fb11270d2..50c04390e 100644 --- a/internal/generator/arithmetic/wave8.go +++ b/internal/generator/arithmetic/wave8.go @@ -76,30 +76,100 @@ func w8BinaryString(n int) string { } func w8Binary(kp int) generator.Problem { - n := w8AR(5, 255) - bin := w8BinaryString(n) - f := map[string]string{"binary": bin} - w8AI(f, "n", n) switch kp { + case 1: - return w8A(kp, fmt.Sprintf("What is the binary numeral \\(%s_2\\) in decimal? (enter a number)", bin), strconv.Itoa(n), - fmt.Sprintf("The place values from the left are powers of two. The digits of %s add to %d.", bin, n), f) + // Converting a numeral (4-bit range: place values 1, 2, 4, 8) + n := w8AR(3, 15) + bin := w8BinaryString(n) + f := map[string]string{"binary": bin} + w8AI(f, "n", n) + + var steps []string + var values []string + for power, m := 0, n; m > 0; power, m = power+1, m>>1 { + bit := m & 1 + val := bit * (1 << power) + steps = append(steps, fmt.Sprintf("\\(%d \\times 2^{%d} = %d\\)", bit, power, val)) + values = append(values, strconv.Itoa(val)) + } + + explanation := fmt.Sprintf( + "Reading from right to left: %s. Adding the results gives \\(%s = %d\\).", + strings.Join(steps, ", "), + strings.Join(values, " + "), + n, + ) + + return w8A( + kp, + fmt.Sprintf("What is the binary numeral \\(%s_2\\) in decimal? (enter a number)", bin), + strconv.Itoa(n), + explanation, + f, + ) + case 2: + // Powers of two as places (up to 5-bit range: powers 1, 2, 4, 8, 16) + n := w8AR(5, 31) + bin := w8BinaryString(n) + f := map[string]string{"binary": bin} + w8AI(f, "n", n) + p := 1 - for p<<1 <= n { + var seq []string + for p <= n { + seq = append(seq, strconv.Itoa(p)) p <<= 1 } - w8AI(f, "power", p) - return w8A(kp, fmt.Sprintf("What is the largest power of two that is at most %d? (enter a number)", n), strconv.Itoa(p), - fmt.Sprintf("Double 1 until the next double would exceed %d. The largest power is %d.", n, p), f) + ans := p >> 1 + seq = append(seq, strconv.Itoa(p)) + w8AI(f, "power", ans) + + explanation := fmt.Sprintf( + "Double 1 repeatedly: \\(%s\\). Since \\(%d \\le %d\\) and \\(%d > %d\\), the largest power of two is %d.", + strings.Join(seq, ", "), + ans, n, p, n, ans, + ) + + return w8A( + kp, + fmt.Sprintf("What is the largest power of two that is at most %d? (enter a number)", n), + strconv.Itoa(ans), + explanation, + f, + ) + default: + // Counting one bits (up to 5-bit range) + n := w8AR(5, 31) + bin := w8BinaryString(n) + f := map[string]string{"binary": bin} + w8AI(f, "n", n) + bits := 0 + var divSteps []string for m := n; m > 0; m >>= 1 { - bits += m & 1 + rem := m & 1 + bits += rem + divSteps = append(divSteps, fmt.Sprintf("\\(%d \\div 2 = %d\\) remainder %d", m, m>>1, rem)) } w8AI(f, "ones", bits) - return w8A(kp, fmt.Sprintf("How many 1 bits are in the binary expansion of %d? (enter a number)", n), strconv.Itoa(bits), - fmt.Sprintf("The binary expansion is %s, which contains %d ones.", bin, bits), f) + + explanation := fmt.Sprintf( + "Halve repeatedly: %s. Reading the remainders from bottom to top gives \\(%s_2\\), which has %d ones.", + strings.Join(divSteps, "; "), + bin, + bits, + ) + + return w8A( + kp, + fmt.Sprintf("How many 1 bits are in the binary expansion of %d? (enter a number)", n), + strconv.Itoa(bits), + explanation, + f, + ) } }