# Skew binary number system

The skew binary number system is a non-standard positional numeral system in which the nth digit contributes a value of $2^{n+1}-1$ times the digit (digits are indexed from 0) instead of $2^{n}$ times as they do in binary. Each digit has a value of 0, 1, or 2. A number can have many skew binary representations. For example, a decimal number 15 can be written as 1000, 201 and 122. Each number can be written uniquely in skew binary canonical form where there is only at most one instance of the digit 2, which must be the least significant nonzero digit. In this case 15 is written canonically as 1000.

## Examples

Canonical skew binary representations of the numbers from 0 to 15 are shown in following table:
| Decimal | Binary | Skew Binary | Ternary |
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 2 | 10 | 2 | 2 |
| 3 | 11 | 10 | 10 |
| 4 | 100 | 11 | 11 |
| 5 | 101 | 12 | 12 |
| 6 | 110 | 20 | 20 |
| 7 | 111 | 100 | 21 |
| 8 | 1000 | 101 | 22 |
| 9 | 1001 | 102 | 100 |
| 10 | 1010 | 110 | 101 |
| 11 | 1011 | 111 | 102 |
| 12 | 1100 | 112 | 110 |
| 13 | 1101 | 120 | 111 |
| 14 | 1110 | 200 | 112 |
| 15 | 1111 | 1000 | 120 |

## Arithmetical operations

The advantage of skew binary is that each increment operation can be done with at most one carry operation. This exploits the fact that $2(2^{n+1}-1)+1=2^{n+2}-1$. Incrementing a skew binary number is done by setting the only two to a zero and incrementing the next digit from zero to one or one to two. When numbers are represented using a form of run-length encoding as linked lists of the non-zero digits, incrementation and decrementation can be performed in constant time.
Other arithmetic operations may be performed by switching between the skew binary representation and the binary representation.

### Conversion between decimal and skew binary number

To convert from decimal to skew binary number, one can use following formula:
Base case: $a(0)=0$
Induction case: $a(2^{n}-1+i)=a(i)+10^{n-1}$
Boundaries: $0\leq i\leq 2^{n}-1,n\geq 1$
To convert from skew binary number to decimal, one can use definition of skew binary number:
$S=\sum _{i=0}^{N}b_{i}(2^{i+1}-1)$, where $b_{i}\in {0,1,2}$, st. only least significant bit (lsb) $b_{lsb}$ is 2.

#### C++ code to convert skew binary number to decimal number
### From skew binary representation to binary representation

Given a skew binary number, its value can be computed by a loop, computing the successive values of $2^{n+1}-1$ and adding it once or twice for each $n$ such that the $n$th digit is 1 or 2 respectively. A more efficient method is now given, with only bit representation and one subtraction.
The skew binary number of the form $b_{0}\dots b_{n}$ without 2 and with $m$ 1s is equal to the binary number $0b_{0}\dots b_{n}$ minus $m$. Let $d^{c}$ represents the digit $d$ repeated $c$ times. The skew binary number of the form $0^{c_{0}}21^{c_{1}}0b_{0}\dots b_{n}$ with $m$ 1s is equal to the binary number $0^{c_{0}+c_{1}+2}1b_{0}\dots b_{n}$ minus $m$.

### From binary representation to skew binary representation

Similarly to the preceding section, the binary number $b$ of the form $b_{0}\dots b_{n}$ with $m$ 1s equals the skew binary number $b_{1}\dots b_{n}$ plus $m$. Note that since addition is not defined, adding $m$ corresponds to incrementing the number $m$ times. However, $m$ is bounded by the logarithm of $b$ and incrementation takes constant time. Hence transforming a binary number into a skew binary number runs in time linear in the length of the number.

## Applications

The skew binary numbers were developed by Eugene Myers in 1983 for a purely functional data structure that allows the operations of the stack abstract data type and also allows efficient indexing into the sequence of stack elements. They were later applied to skew binomial heaps, a variant of binomial heaps that support constant-time worst-case insertion operations.