Skew binary number system

Jump to navigation Jump to search

The skew binary number system is a non-standard positional numeral system in which the nth digit contributes a value of ${\displaystyle 2^{n+1}-1}$ times the digit (digits are indexed from 0) instead of ${\displaystyle 2^{n}}$ times as they do in binary. Each digit has a value of 0, 1, or 2. Notice that 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 first non-zero least significant digit. In this case 15 is written canonically as 1000.

Examples

Skew binary representations of the numbers from 0 to 15 are shown in following table:

Decimal Skew binary binary
0 0 0
1 1 1
2 2 10
3 10 11
4 11 100
5 12 101
6 20 110
7 100 111
8 101 1000
9 102 1001
10 110 1010
11 111 1011
12 112 1100
13 120 1101
14 200 1110
15 1000 1111

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 ${\displaystyle 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.[1] When numbers are represented using a form of run-length encoding as linked lists of the non-zero digits, a form called the sparse representations of numbers by Okasaki,[2] incrementation and decrementation can be performed in constant time.

Other arithmetic operations are performed[3] by switching between the skew binary representation and the binary representation.

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 ${\displaystyle 2^{n+1}-1}$ and adding it once or twice for each ${\displaystyle n}$ such that the ${\displaystyle n}$th digit is 1 or 2 respectively. A more efficient method is now given, with only bit representation and one substraction.

The skew binary number of the form ${\displaystyle b_{0}\dots b_{n}}$ without 2 and with ${\displaystyle m}$ 1s is equal to the binary number ${\displaystyle 0b_{0}\dots b_{n}}$ minus ${\displaystyle m}$. Let ${\displaystyle d^{c}}$ represents the digit ${\displaystyle d}$ repeated ${\displaystyle c}$ times. The skew binary number of the form ${\displaystyle 0^{c_{0}}21^{c_{1}}0b_{0}\dots b_{n}}$ with ${\displaystyle m}$ 1s is equal to the binary number ${\displaystyle 0^{c_{0}+c_{1}+2}1b_{0}\dots b_{n}}$ minus ${\displaystyle m}$.

From binary representation to skew binary representation

Similarly to the preceding section, the binary number ${\displaystyle b}$ of the form ${\displaystyle b_{0}\dots b_{n}}$ with ${\displaystyle m}$ 1s equals the skew binary number ${\displaystyle b_{1}\dots b_{n}}$ plus ${\displaystyle m}$. Note that since addition is not defined, adding ${\displaystyle m}$ corresponds to incrementing the number ${\displaystyle m}$ times. However, ${\displaystyle m}$ is bounded by the logarithm of ${\displaystyle 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

Skew binary numbers find applications in skew binomial heaps, a variant of binomial heaps that support worst-case O(1) insertion, and in skew binary random access lists, a purely functional data structure. They also find use in bootstrapped skew binomial heaps, which have excellent asymptotic guarantees.[2]

If smoothsort is implemented using perfect binary trees (rather than the more common Leonardo trees), the heap is divided into trees which correspond to the digits of the skew binary representation of the heap size.

Notes

1. ^ skew binary numbers
2. ^ a b Okasaki, Chris. Purely Functional Data Structures.
3. ^ Elmasry, Amr; Jensen, Claus; Katajainen, Jyrki (2012). "Two Skew-Binary Numeral Systems and One Application" (PDF). Theory of Computing Systems. 50: 185–211. doi:10.1007/s00224-011-9357-0.