Wolfram Research

Function Repository Resource:

ToNegabinary

Source Notebook

Get the negabinary representation of an integer

Contributed by: Eric W. Weisstein

ResourceFunction["ToNegabinary"][n]

gives the negabinary representation of the integer n.

Details and Options

Negabinary is a representation of an integer in base -2.

Examples

Basic Examples

Get the representation for 17 in negabinary:

In[1]:=
ResourceFunction["ToNegabinary"][17]
Out[1]=

From this representation, recover the original integer:

In[2]:=
Table[%[[-n]] (-2)^(n - 1), {n, 5}]
Out[2]=
In[3]:=
Plus @@ %
Out[3]=

Using FromNegabinary:

In[4]:=
ResourceFunction["FromNegabinary"][
 ResourceFunction["ToNegabinary"][17]]
Out[4]=

Some integers have the same representation in base 2:

In[5]:=
IntegerDigits[17, 2]
Out[5]=

Plot the negabinary representation for the first 22 integers:

In[6]:=
Rotate[ArrayPlot[Table[ResourceFunction["ToNegabinary"][n], {n, 85}], ColorRules -> {0 -> Gray}, Frame -> False], \[Pi]/2]
Out[6]=
In[7]:=
Grid[Join[{{"n", "(-2)^4=16", "(-2)^3=-8", "(-2)^2=4", "(-2)^1=-2", "negabinary"}}, Table[Flatten[{n, PadLeft[ResourceFunction["ToNegabinary"][n], 4], Row[ResourceFunction["ToNegabinary"][n]]}], {n, -5, 5}]], Frame -> All]
Out[7]=

Scope

ToNegabinary handles negative numbers:

In[8]:=
ResourceFunction["ToNegabinary"][-5]
Out[8]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information