User:Superm401/decimal to binary

From Wikipedia, the free encyclopedia


[edit] Description

This will convert a decimal numeral into a binary one. For example:

{{User:Superm401/decimal to binary|57}} produces: 00000000000000000000000000111001

and

{{User:Superm401/decimal to binary|4294967295}} produces: 11111111111111111111111111111111

Because templates do not allow internal variables, the template calculates every digit of the result independently then simply concatenates them together. This means the value of higher digits must always be subtracted using the mod operator. Then, the remaining value is divided by the appropriate power of 2, then truncated to 0 or 1. Because the truncation sometimes returns floating point values instead of 1 (e.g. .999999901), it was necessary to correct this by assigning either 1 or 0 with ifexpr, depending on whether the value is greater than 0.5.