To convert a number from base 10 to base 2 in javascript, we can use the bitwise shift operator >>
to extract the binary digits from the number. Here's an algorithm to perform the conversion:
&
with 1 to extract the rightmost binary digit>>
to shift the input number one bit to the rightHere's the code implementation of this algorithm in JavaScript:
index.tsx372 chars12 lines
Example usage:
index.tsx118 chars4 lines
gistlibby LogSnag