Difference between revisions of "Template:Binary"
Jump to navigation
Jump to search
imported>Vanisaac (updated template to one based on template:octal to allow x1024 range (max input value now 134,217,727 instead of 131,071)) |
imported>Vanisaac |
||
| Line 1: | Line 1: | ||
| − | <includeonly>{{#ifeq:{{#expr: {{{1}}} > 134217727}} | 1 |N/A|{{#ifeq:{{#expr: {{{1}}} < 0}} | 1 |N/A|<!-- | + | <!-- |
| − | -->{{#switch:{{#expr: ({{{1}}} div 16777216) mod 8}}|0={{#ifeq:{{#expr: {{{1}}} >134217727}} | 1 |000}}|1={{#ifeq:{{#expr: {{{1}}} >134217727}} | 1 |001|1}}|2={{#ifeq:{{#expr: {{{1}}} >134217727}} | 1 |010|10}}|3={{#ifeq:{{#expr: {{{1}}} >134217727}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | + | Integer part - everything acts on #expr: trunc{{{1}}}. |
| − | -->{{#switch:{{#expr: ({{{1}}} div 2097152) mod 8}}|0={{#ifeq:{{#expr: {{{1}}} >16777215}} | 1 |000}}|1={{#ifeq:{{#expr: {{{1}}} >16777215}} | 1 |001|1}}|2={{#ifeq:{{#expr: {{{1}}} >16777215}} | 1 |010|10}}|3={{#ifeq:{{#expr: {{{1}}} >16777215}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | + | All values are calculated as octal digits, displayed as binary. |
| − | -->{{#switch:{{#expr: ({{{1}}} div 262144) mod 8}}|0={{#ifeq:{{#expr: {{{1}}} >2097151}} | 1 |000}}|1={{#ifeq:{{#expr: {{{1}}} >2097151}} | 1 |001|1}}|2={{#ifeq:{{#expr: {{{1}}} >2097151}} | 1 |010|10}}|3={{#ifeq:{{#expr: {{{1}}} >2097151}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | + | |
| − | -->{{#switch:{{#expr: ({{{1}}} div 32768) mod 8}}|0={{#ifeq:{{#expr: {{{1}}} >262143}} | 1 |000}}|1={{#ifeq:{{#expr: {{{1}}} >262143}} | 1 |001|1}}|2={{#ifeq:{{#expr: {{{1}}} >262143}} | 1 |010|10}}|3={{#ifeq:{{#expr: {{{1}}} >262143}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | + | --><includeonly>{{#ifeq:{{#expr: trunc{{{1}}} > 134217727}} | 1 |N/A|{{#ifeq:{{#expr: trunc{{{1}}} < 0}} | 1 |N/A|<!-- |
| − | -->{{#switch:{{#expr: ({{{1}}} div 4096) mod 8}}|0={{#ifeq:{{#expr: {{{1}}} >32767}} | 1 |000}}|1={{#ifeq:{{#expr: {{{1}}} >32767}} | 1 |001|1}}|2={{#ifeq:{{#expr: {{{1}}} >32767}} | 1 |010|10}}|3={{#ifeq:{{#expr: {{{1}}} >32767}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | + | -->{{#switch:{{#expr: (trunc{{{1}}} div 16777216) mod 8}}|0={{#ifeq:{{#expr: trunc{{{1}}} >134217727}} | 1 |000}}|1={{#ifeq:{{#expr: trunc{{{1}}} >134217727}} | 1 |001|1}}|2={{#ifeq:{{#expr: trunc{{{1}}} >134217727}} | 1 |010|10}}|3={{#ifeq:{{#expr: trunc{{{1}}} >134217727}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- |
| − | -->{{#switch:{{#expr: ({{{1}}} div 512) mod 8}}|0={{#ifeq:{{#expr: {{{1}}} >4095}} | 1 |000}}|1={{#ifeq:{{#expr: {{{1}}} >4095}} | 1 |001|1}}|2={{#ifeq:{{#expr: {{{1}}} >4095}} | 1 |010|10}}|3={{#ifeq:{{#expr: {{{1}}} >4095}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | + | -->{{#switch:{{#expr: (trunc{{{1}}} div 2097152) mod 8}}|0={{#ifeq:{{#expr: trunc{{{1}}} >16777215}} | 1 |000}}|1={{#ifeq:{{#expr: trunc{{{1}}} >16777215}} | 1 |001|1}}|2={{#ifeq:{{#expr: trunc{{{1}}} >16777215}} | 1 |010|10}}|3={{#ifeq:{{#expr: trunc{{{1}}} >16777215}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- |
| − | -->{{#switch:{{#expr: ({{{1}}} div 64) mod 8}}|0={{#ifeq:{{#expr: {{{1}}} >511}} | 1 |000}}|1={{#ifeq:{{#expr: {{{1}}} >511}} | 1 |001|1}}|2={{#ifeq:{{#expr: {{{1}}} >511}} | 1 |010|10}}|3={{#ifeq:{{#expr: {{{1}}} >511}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | + | -->{{#switch:{{#expr: (trunc{{{1}}} div 262144) mod 8}}|0={{#ifeq:{{#expr: trunc{{{1}}} >2097151}} | 1 |000}}|1={{#ifeq:{{#expr: trunc{{{1}}} >2097151}} | 1 |001|1}}|2={{#ifeq:{{#expr: trunc{{{1}}} >2097151}} | 1 |010|10}}|3={{#ifeq:{{#expr: trunc{{{1}}} >2097151}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- |
| − | -->{{#switch:{{#expr: ({{{1}}} div 8) mod 8}}|0={{#ifeq:{{#expr: {{{1}}} >63}} | 1 |000}}|1={{#ifeq:{{#expr: {{{1}}} >63}} | 1 |001|1}}|2={{#ifeq:{{#expr: {{{1}}} >63}} | 1 |010|10}}|3={{#ifeq:{{#expr: {{{1}}} >63}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | + | -->{{#switch:{{#expr: (trunc{{{1}}} div 32768) mod 8}}|0={{#ifeq:{{#expr: trunc{{{1}}} >262143}} | 1 |000}}|1={{#ifeq:{{#expr: trunc{{{1}}} >262143}} | 1 |001|1}}|2={{#ifeq:{{#expr: trunc{{{1}}} >262143}} | 1 |010|10}}|3={{#ifeq:{{#expr: trunc{{{1}}} >262143}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- |
| − | -->{{#switch:{{#expr: ({{{1}}} div 1) mod 8}}|0={{#ifeq:{{#expr: {{{1}}} >7}} | 1 |000|0}}|1={{#ifeq:{{#expr: {{{1}}} >7}} | 1 |001|1}}|2={{#ifeq:{{#expr: {{{1}}} >7}} | 1 |010|10}}|3={{#ifeq:{{#expr: {{{1}}} >7}} | 1 |011|11}}|4=100|5=101|6=110|7=111}} | + | -->{{#switch:{{#expr: (trunc{{{1}}} div 4096) mod 8}}|0={{#ifeq:{{#expr: trunc{{{1}}} >32767}} | 1 |000}}|1={{#ifeq:{{#expr: trunc{{{1}}} >32767}} | 1 |001|1}}|2={{#ifeq:{{#expr: trunc{{{1}}} >32767}} | 1 |010|10}}|3={{#ifeq:{{#expr: trunc{{{1}}} >32767}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- |
| − | <noinclude> | + | -->{{#switch:{{#expr: (trunc{{{1}}} div 512) mod 8}}|0={{#ifeq:{{#expr: trunc{{{1}}} >4095}} | 1 |000}}|1={{#ifeq:{{#expr: trunc{{{1}}} >4095}} | 1 |001|1}}|2={{#ifeq:{{#expr: trunc{{{1}}} >4095}} | 1 |010|10}}|3={{#ifeq:{{#expr: trunc{{{1}}} >4095}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- |
| + | -->{{#switch:{{#expr: (trunc{{{1}}} div 64) mod 8}}|0={{#ifeq:{{#expr: trunc{{{1}}} >511}} | 1 |000}}|1={{#ifeq:{{#expr: trunc{{{1}}} >511}} | 1 |001|1}}|2={{#ifeq:{{#expr: trunc{{{1}}} >511}} | 1 |010|10}}|3={{#ifeq:{{#expr: trunc{{{1}}} >511}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | ||
| + | -->{{#switch:{{#expr: (trunc{{{1}}} div 8) mod 8}}|0={{#ifeq:{{#expr: trunc{{{1}}} >63}} | 1 |000}}|1={{#ifeq:{{#expr: trunc{{{1}}} >63}} | 1 |001|1}}|2={{#ifeq:{{#expr: trunc{{{1}}} >63}} | 1 |010|10}}|3={{#ifeq:{{#expr: trunc{{{1}}} >63}} | 1 |011|11}}|4=100|5=101|6=110|7=111}}<!-- | ||
| + | -->{{#switch:{{#expr: (trunc{{{1}}} div 1) mod 8}}|0={{#ifeq:{{#expr: trunc{{{1}}} >7}} | 1 |000|0}}|1={{#ifeq:{{#expr: trunc{{{1}}} >7}} | 1 |001|1}}|2={{#ifeq:{{#expr: trunc{{{1}}} >7}} | 1 |010|10}}|3={{#ifeq:{{#expr: trunc{{{1}}} >7}} | 1 |011|11}}|4=100|5=101|6=110|7=111}} }} }}<!-- | ||
| + | Decimal part - everything acts on #expr: ({{{1}}}-trunc{{{1}}}) | ||
| + | All values calculated as individual binary digits. Automatically truncates at last significant digit, unless template call specifies 0 padding. | ||
| + | |||
| + | -->{{#ifeq: {{{1|}}} | {{#expr:trunc{{{1|}}}}} | {{#if:{{{2|}}}|.{{padleft:0|{{{2}}}|0}}|}} |<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 0}} | 1 | .{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^1 mod 2}} | }}<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 1}} | 1 | {{#ifeq: {{#expr: ({{{1}}}-trunc{{{1}}})*2^1}} |{{#expr: trunc(({{{1}}}-trunc{{{1}}})*2^1)}}| {{#ifeq: {{#expr: {{{2|}}} > 1}} | 1 | 0 }} |{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^2 mod 2}} | }}}}<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 2}} | 1 | {{#ifeq: {{#expr: ({{{1}}}-trunc{{{1}}})*2^2}} |{{#expr: trunc(({{{1}}}-trunc{{{1}}})*2^2)}}| {{#ifeq: {{#expr: {{{2|}}} > 2}} | 1 | 0 }} |{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^3 mod 2}} | }}}}<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 3}} | 1 | {{#ifeq: {{#expr: ({{{1}}}-trunc{{{1}}})*2^3}} |{{#expr: trunc(({{{1}}}-trunc{{{1}}})*2^3)}}| {{#ifeq: {{#expr: {{{2|}}} > 3}} | 1 | 0 }} |{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^4 mod 2}} | }}}}<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 4}} | 1 | {{#ifeq: {{#expr: ({{{1}}}-trunc{{{1}}})*2^4}} |{{#expr: trunc(({{{1}}}-trunc{{{1}}})*2^4)}}| {{#ifeq: {{#expr: {{{2|}}} > 4}} | 1 | 0 }} |{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^5 mod 2}} | }}}}<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 5}} | 1 | {{#ifeq: {{#expr: ({{{1}}}-trunc{{{1}}})*2^5}} |{{#expr: trunc(({{{1}}}-trunc{{{1}}})*2^5)}}| {{#ifeq: {{#expr: {{{2|}}} > 5}} | 1 | 0 }} |{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^6 mod 2}} | }}}}<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 6}} | 1 | {{#ifeq: {{#expr: ({{{1}}}-trunc{{{1}}})*2^6}} |{{#expr: trunc(({{{1}}}-trunc{{{1}}})*2^6)}}| {{#ifeq: {{#expr: {{{2|}}} > 6}} | 1 | 0 }} |{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^7 mod 2}} | }}}}<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 7}} | 1 | {{#ifeq: {{#expr: ({{{1}}}-trunc{{{1}}})*2^7}} |{{#expr: trunc(({{{1}}}-trunc{{{1}}})*2^7)}}| {{#ifeq: {{#expr: {{{2|}}} > 7}} | 1 | 0 }} |{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^8 mod 2}} | }}}}<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 8}} | 1 | {{#ifeq: {{#expr: ({{{1}}}-trunc{{{1}}})*2^8}} |{{#expr: trunc(({{{1}}}-trunc{{{1}}})*2^8)}}| {{#ifeq: {{#expr: {{{2|}}} > 8}} | 1 | 0 }} |{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^9 mod 2}} | }}}}<!-- | ||
| + | -->{{#ifeq: {{#expr: {{{2|10}}} > 9}} | 1 | {{#ifeq: {{#expr: ({{{1}}}-trunc{{{1}}})*2^9}} |{{#expr: trunc(({{{1}}}-trunc{{{1}}})*2^9)}}| {{#ifeq: {{#expr: {{{2|}}} > 9}} | 1 | 0 }} |{{#expr: ({{{1}}}-trunc{{{1}}}) * 2^10 mod 2}} | }}}}<!-- | ||
| + | -->}}<sub>2</sub></includeonly><noinclude> | ||
{{documentation}} | {{documentation}} | ||
</noinclude> | </noinclude> | ||
Revision as of 10:33, 21 July 2011
| 30px | This template uses Lua: |
This template is for quickly converting a decimal number to binary.
Usage
Use {{Binary|x|y}} where x is the decimal number and y is the decimal precision (positive numbers, defaults displays up to 10 digits following the binary point).
Examples:
| Code | Result | Notes |
|---|---|---|
{{Binary|128892}} |
111110111011111002 | Regular integer |
{{Binary|73.3333333|5}} |
1001001.010102 | Truncated decimal |
{{Binary|73.3333333|0}} |
10010012 | Truncated at decimal point |
{{Binary|73|5}} |
1001001.000002 | Decimals padded to integer |
{{Binary|73.25}} |
1001001.012 | Auto truncation |
{{Binary|73.25|4}} |
1001001.01002 | Decimals padded |
{{Binary|{{#expr:1+3/8}}}} |
1.0112 | Expression support |
See also
| The above documentation is transcluded from Template:Binary/doc. (edit | history) Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages. Please add categories to the /doc subpage. Subpages of this template. |