How does repeatedly taking `num % 2` and `num // 2` peel a base-10 integer into its binary digits — and why do negatives need no extra work?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
In the integer-to-binary algorithm, each step grabs the next bit with the remainder
operator %. The current number is 13. Compute the remainder when it is divided by 2
and assign that bit to bit.
(Hint: for any whole number, n % 2 is 0 when it is even and 1 when it is odd.)
Adding 0.1 ten times to x
Write the loop that adds 0.1 to x ten times, record the printed value 0.9999999999999999, and note that x == 1 evaluates to False.
The remainder-and-integer-division algorithm
Write the loop that prepends num % 2 to a string and updates num = num // 2 while num > 0, and trace 19 through 9, 4, 2, 1 to 10011.
Handling a negative input with a sign flag
State the opening if/else that sets a negative flag and replaces the number by its absolute value, then prepends a minus sign at the end, giving -10011 for -19.