Converting a base 10 number to a base 2 number

This videos shows step by step how to convert a decimal number to a binary number, first dividing the given decimal number by 2, followed by the division of the quotient by 2 again until the quotient is less than 2. The final binary is given by the remainders of the divisions in which the remainder of the last division is the Most Significant Bit (MSB) and the remainder of the first division is the Least Significant Bit (LSB).

DecToBin

Run by yourself

To run it, please use the Interactive Notebook provided by Manim Community. First run the cell for importing manim library (from manim import *), copy and paste the content below in another cell and run it. You can change the variable dividend to another value you want.

from manim import *
%%manim DecToBin -ql -v ERROR

class DecToBin(Scene):
    def print_result(self, dividend, divisor, dd, dv, rlist):
        quotient = dividend // divisor
        remainder = dividend % divisor

        re = Tex(str(remainder)).next_to(dd, DOWN, buff=MED_SMALL_BUFF)
        rlist.append(re)
        qu = Tex(str(quotient)).next_to(dv, DOWN, buff=MED_SMALL_BUFF)
        self.play(Write(qu))
        self.play(Write(re))

        ndv = Tex(str(divisor)).next_to(qu, RIGHT, buff=MED_SMALL_BUFF)
        if quotient >= divisor:
            semi_box = SemiBox(ndv, color = YELLOW)
            self.play(ShowCreation(semi_box))
            self.play(Write(ndv))

        return quotient, qu, ndv

    def construct(self):
        dividend = 67
        divisor = 2

        dd = Tex(str(dividend))
        self.play(Write(dd))
        self.play(dd.animate.to_corner(UP + LEFT))
        dv = Tex(str(divisor)).next_to(dd, RIGHT, buff=MED_SMALL_BUFF)
        semi_box = SemiBox(dv, color = YELLOW)
        self.play(ShowCreation(semi_box))
        self.play(Write(dv))

        rlist = []
        quotient, qu, ndv = self.print_result(dividend, divisor, dd, dv, rlist)
        while quotient >= divisor:
            quotient, qu, ndv = self.print_result(quotient, divisor, qu, ndv, rlist);
        bin = qu.copy()

        rect = SurroundingRectangle(qu, color = PINK)
        self.play(ShowCreation(rect))
        clist = [bin]
        for r in reversed(rlist):
            rect = SurroundingRectangle(r, color = PINK)
            self.play(ShowCreation(rect))
            rem = r.copy()
            clist.append(rem)

        for c in clist:
            c.generate_target()
            if clist.index(c) == 0:
                c.target.move_to(ORIGIN)
            else:
                c.target.next_to(clist[clist.index(c)-1], RIGHT)
            c.set_height(2*c.get_height())
            self.play(
                MoveToTarget(c)
            )
            self.play(
                c.animate.scale(1.5)
            )

        amsb = Arrow(start=UP, end=DOWN, buff=SMALL_BUFF).next_to(clist[0], DOWN)
        msb = Tex(r"MSB").next_to(amsb, DOWN)
        tmsb = Tex(r"Most~Significant~Bit")
        tmsb.next_to(msb, DOWN)
        tmsb.scale(0.8)
        amsb.set_color(GREEN)
        msb.set_color(GREEN)
        tmsb.set_color(GREEN)
        self.play(
            ShowCreation(amsb),
            ShowCreation(msb),
            ShowCreation(tmsb),
            tmsb.animate.shift(LEFT),
            clist[0].animate.scale(1.5)
        )

        alsb = Arrow(start=UP, end=DOWN, buff=SMALL_BUFF).next_to(clist[-1], DOWN)
        lsb = Tex(r"LSB").next_to(alsb, DOWN)
        tlsb = Tex(r"Least~Significant~Bit")
        tlsb.scale(0.8)
        tlsb.next_to(lsb, DOWN)
        alsb.set_color(BLUE)
        lsb.set_color(BLUE)
        tlsb.set_color(BLUE)
        self.play(
            ShowCreation(alsb),
            ShowCreation(lsb),
            ShowCreation(tlsb),
            tlsb.animate.shift(RIGHT*1.5),
            clist[-1].animate.scale(0.75)
        )

        self.wait()
        
class SemiBox(VGroup):
    def __init__(self, mobject, **kwargs):
        VGroup.__init__(self, **kwargs)
        hline = Line(start=[-0.2,0,0], end=[mobject.get_width(),0,0], stroke_color=YELLOW).next_to(mobject, DOWN, buff=SMALL_BUFF)
        hline.set_stroke(self.stroke_color)
        vline = Line(start=[0,-0.2,0], end=[0,mobject.get_height(),0], stroke_color=YELLOW).next_to(mobject, LEFT, buff=SMALL_BUFF)
        vline.set_stroke(self.stroke_color)
        self.add(hline, vline)
<string>:99: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:101: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:99: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:101: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:99: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:101: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:99: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:101: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:99: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:101: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:99: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:101: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer setting the attribute normally or with Mobject.set().
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer setting the attribute normally or with Mobject.set().
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer setting the attribute normally or with Mobject.set().
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer setting the attribute normally or with Mobject.set().
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer setting the attribute normally or with Mobject.set().
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer setting the attribute normally or with Mobject.set().
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer getting the attribute normally.
<string>:54: DeprecationWarning: This method is not guaranteed to stay around. Please prefer setting the attribute normally or with Mobject.set().
                                                                                         

This notebook uses Manim. Manim is an animation engine for explanatory math videos. It's used to create precise animations programmatically, as demonstrated in the videos of 3Blue1Brown.

Animations created by Cleber Jorge Amaral.