Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

66%
+2 −0
Q&A A formal-logic formula for decimal to binary conversion

posted 3y ago by r~~‭  ·  edited 3y ago by r~~‭

Answer
#4: Post edited by user avatar r~~‭ · 2021-07-10T22:33:19Z (almost 3 years ago)
  • If you have a non-negative integer $n$, and the base $b$ representation of $n$ is a sequence of digits, numbered from the right as $d_k\dots d_2d_1d_0$, then $d_i = \left\lfloor \frac{n}{b^i} ight floor \mod b$.
  • For example, decimal 10 is binary 1010 because:
  • $$
  • d_3 = \left\lfloor \frac{10}{2^3} ight floor \pmod 2 = 1 \pmod 2 = 1
  • $$
  • $$
  • d_2 = \left\lfloor \frac{10}{2^2} ight floor \pmod 2 = 2 \pmod 2 = 0
  • $$
  • $$
  • d_1 = \left\lfloor \frac{10}{2^1} ight floor \pmod 2 = 5 \pmod 2 = 1
  • $$
  • $$
  • d_0 = \left\lfloor \frac{10}{2^0} ight floor \pmod 2 = 10 \pmod 2 = 0
  • $$
  • (For all $i > 3$, $d_i = 0$, because $10 < 2^i$.)
  • This can be computed more efficiently using a loop or recursion, but this is the more succinct mathematical description.
  • By the way, extending this formula to non-integers is straightforward—just continue the digit numbering past the point as $d_k\dots d_2d_1d_0.d_{-1}d_{-2}\dots$, and the formula is still valid, modulo the usual philosophical concerns about infinite sequences of digits.
  • If you have a non-negative integer $n$, and the base $b$ representation of $n$ is a sequence of digits, numbered from the right as $d_k\dots d_2d_1d_0$, then $d_i = \left\lfloor \frac{n}{b^i} ight floor \bmod b$.
  • For example, decimal 10 is binary 1010 because:
  • $$
  • d_3 = \left\lfloor \frac{10}{2^3} ight floor \bmod 2 = 1 \bmod 2 = 1
  • $$
  • $$
  • d_2 = \left\lfloor \frac{10}{2^2} ight floor \bmod 2 = 2 \bmod 2 = 0
  • $$
  • $$
  • d_1 = \left\lfloor \frac{10}{2^1} ight floor \bmod 2 = 5 \bmod 2 = 1
  • $$
  • $$
  • d_0 = \left\lfloor \frac{10}{2^0} ight floor \bmod 2 = 10 \bmod 2 = 0
  • $$
  • (For all $i > 3$, $d_i = 0$, because $10 < 2^i$.)
  • This can be computed more efficiently using a loop or recursion, but this is the more succinct mathematical description.
  • By the way, extending this formula to non-integers is straightforward—just continue the digit numbering past the point as $d_k\dots d_2d_1d_0.d_{-1}d_{-2}\dots$, and the formula is still valid, modulo the usual philosophical concerns about infinite sequences of digits.
#3: Post edited by user avatar r~~‭ · 2021-07-10T17:56:00Z (almost 3 years ago)
  • If you have a non-negative integer $n$, and the base $b$ representation of $n$ is a sequence of digits, numbered from the right as $d_k\dots d_2d_1d_0$, then $d_i = \left\lfloor \frac{n}{b^i} \right\rfloor \mod b$.
  • For example, decimal 10 is binary 1010 because:
  • $$
  • d_3 = \left\lfloor \frac{10}{2^3} \right\rfloor \pmod 2 = 1 \pmod 2 = 1
  • $$
  • $$
  • d_2 = \left\lfloor \frac{10}{2^2} \right\rfloor \pmod 2 = 2 \pmod 2 = 0
  • $$
  • $$
  • d_1 = \left\lfloor \frac{10}{2^1} \right\rfloor \pmod 2 = 5 \pmod 2 = 1
  • $$
  • $$
  • d_0 = \left\lfloor \frac{10}{2^0} \right\rfloor \pmod 2 = 10 \pmod 2 = 0
  • $$
  • (For all $i > 3$, $d_i = 0$, because $10 < 2^i$.)
  • This can be computed more efficiently using a loop or recursion, but this is the more succinct mathematical description.
  • By the way, extending this formula to non-integers is straightforward—continue the digit numbering past the point as $d_k\dots d_2d_1d_0.d_{-1}d_{-2}\dots$, and put a final floor around the formula: $d_i = \left\lfloor\left\lfloor \frac{n}{b^i} \right\rfloor \mod b\right\rfloor$.
  • If you have a non-negative integer $n$, and the base $b$ representation of $n$ is a sequence of digits, numbered from the right as $d_k\dots d_2d_1d_0$, then $d_i = \left\lfloor \frac{n}{b^i} \right\rfloor \mod b$.
  • For example, decimal 10 is binary 1010 because:
  • $$
  • d_3 = \left\lfloor \frac{10}{2^3} \right\rfloor \pmod 2 = 1 \pmod 2 = 1
  • $$
  • $$
  • d_2 = \left\lfloor \frac{10}{2^2} \right\rfloor \pmod 2 = 2 \pmod 2 = 0
  • $$
  • $$
  • d_1 = \left\lfloor \frac{10}{2^1} \right\rfloor \pmod 2 = 5 \pmod 2 = 1
  • $$
  • $$
  • d_0 = \left\lfloor \frac{10}{2^0} \right\rfloor \pmod 2 = 10 \pmod 2 = 0
  • $$
  • (For all $i > 3$, $d_i = 0$, because $10 < 2^i$.)
  • This can be computed more efficiently using a loop or recursion, but this is the more succinct mathematical description.
  • By the way, extending this formula to non-integers is straightforward—just continue the digit numbering past the point as $d_k\dots d_2d_1d_0.d_{-1}d_{-2}\dots$, and the formula is still valid, modulo the usual philosophical concerns about infinite sequences of digits.
#2: Post edited by user avatar r~~‭ · 2021-07-10T17:37:09Z (almost 3 years ago)
  • If you have a number $n$, and the base $b$ representation of $n$ is a sequence of digits, numbered from the right as $d_k\dots d_2d_1d_0$, then $d_i = \left\lfloor \frac{n}{b^i} ight floor \mod b$.
  • For example, decimal 10 is binary 1010 because:
  • $$
  • d_3 = \left\lfloor \frac{10}{2^3} \right\rfloor \pmod 2 = 1 \pmod 2 = 1
  • $$
  • $$
  • d_2 = \left\lfloor \frac{10}{2^2} \right\rfloor \pmod 2 = 2 \pmod 2 = 0
  • $$
  • $$
  • d_1 = \left\lfloor \frac{10}{2^1} \right\rfloor \pmod 2 = 5 \pmod 2 = 1
  • $$
  • $$
  • d_0 = \left\lfloor \frac{10}{2^0} \right\rfloor \pmod 2 = 10 \pmod 2 = 0
  • $$
  • (For all $i > 3$, $d_i = 0$, because $10 < 2^i$.)
  • This can be computed more efficiently using a loop or recursion, but this is the more succinct mathematical description.
  • If you have a non-negative integer $n$, and the base $b$ representation of $n$ is a sequence of digits, numbered from the right as $d_k\dots d_2d_1d_0$, then $d_i = \left\lfloor \frac{n}{b^i} ight floor \mod b$.
  • For example, decimal 10 is binary 1010 because:
  • $$
  • d_3 = \left\lfloor \frac{10}{2^3} \right\rfloor \pmod 2 = 1 \pmod 2 = 1
  • $$
  • $$
  • d_2 = \left\lfloor \frac{10}{2^2} \right\rfloor \pmod 2 = 2 \pmod 2 = 0
  • $$
  • $$
  • d_1 = \left\lfloor \frac{10}{2^1} \right\rfloor \pmod 2 = 5 \pmod 2 = 1
  • $$
  • $$
  • d_0 = \left\lfloor \frac{10}{2^0} \right\rfloor \pmod 2 = 10 \pmod 2 = 0
  • $$
  • (For all $i > 3$, $d_i = 0$, because $10 < 2^i$.)
  • This can be computed more efficiently using a loop or recursion, but this is the more succinct mathematical description.
  • By the way, extending this formula to non-integers is straightforward—continue the digit numbering past the point as $d_k\dots d_2d_1d_0.d_{-1}d_{-2}\dots$, and put a final floor around the formula: $d_i = \left\lfloor\left\lfloor \frac{n}{b^i} \right\rfloor \mod b\right\rfloor$.
#1: Initial revision by user avatar r~~‭ · 2021-07-10T17:32:04Z (almost 3 years ago)
If you have a number $n$, and the base $b$ representation of $n$ is a sequence of digits, numbered from the right as $d_k\dots d_2d_1d_0$, then $d_i = \left\lfloor \frac{n}{b^i} \right\rfloor \mod b$.

For example, decimal 10 is binary 1010 because:

$$
d_3 = \left\lfloor \frac{10}{2^3} \right\rfloor \pmod 2 = 1 \pmod 2 = 1
$$
$$
d_2 = \left\lfloor \frac{10}{2^2} \right\rfloor \pmod 2 = 2 \pmod 2 = 0
$$
$$
d_1 = \left\lfloor \frac{10}{2^1} \right\rfloor \pmod 2 = 5 \pmod 2 = 1
$$
$$
d_0 = \left\lfloor \frac{10}{2^0} \right\rfloor \pmod 2 = 10 \pmod 2 = 0
$$

(For all $i > 3$, $d_i = 0$, because $10 < 2^i$.)

This can be computed more efficiently using a loop or recursion, but this is the more succinct mathematical description.