Difference between revisions of "Template:Binary"

From blackwiki
Jump to navigation Jump to search
imported>Vanisaac
(Added support for decimals. (w/ previous edit))
imported>Primefac
m (Protected "Template:Binary": semi-protecting highly-visible templates in response to recent template-space vandalism ([Edit=Require autoconfirmed or confirmed access] (indefinite) [Move=Require autoconfirmed or confirmed access] (indefi...)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
<!--Integer part - everything acts on #expr: trunc{{{1}}}.
+
<includeonly>{{#invoke: BaseConvert
All values are calculated as octal digits, displayed as binary.
+
  | convert
 
+
  | n={{{1}}}  
--><includeonly>{{#ifeq:{{#expr: trunc{{{1}}} > 134217727}} | 1 |N/A|{{#ifeq:{{#expr: trunc{{{1}}} < 0}} | 1 |N/A|<!--
+
  | base=2  
-->{{#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}}<!--
+
  | precision={{{2}}}  
-->{{#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}}<!--
+
  | suffix=<sub>2</sub>
-->{{#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}}<!--
+
}}</includeonly><noinclude>
-->{{#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: (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}}<!--
 
-->{{#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>

Latest revision as of 19:25, 10 January 2018

50px Template documentation[view] [edit] [history] [purge]

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