Canonical skew binary representations of the numbers from 0 to 15 are shown in following table:1
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 {\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. 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.2
To convert from decimal to skew binary number, one can use following formula:3
Base case: a ( 0 ) = 0 {\displaystyle a(0)=0}
Induction case: a ( 2 n − 1 + i ) = a ( i ) + 10 n − 1 {\displaystyle a(2^{n}-1+i)=a(i)+10^{n-1}}
Boundaries: 0 ≤ i ≤ 2 n − 1 , n ≥ 1 {\displaystyle 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 = ∑ i = 0 N b i ( 2 i + 1 − 1 ) {\displaystyle S=\sum _{i=0}^{N}b_{i}(2^{i+1}-1)} , where b i ∈ 0 , 1 , 2 {\displaystyle b_{i}\in {0,1,2}} , st. only least significant bit (lsb) b l s b {\displaystyle b_{lsb}} is 2.
Given a skew binary number, its value can be computed by a loop, computing the successive values of 2 n + 1 − 1 {\displaystyle 2^{n+1}-1} and adding it once or twice for each n {\displaystyle n} such that the n {\displaystyle 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 … b n {\displaystyle b_{0}\dots b_{n}} without 2 and with m {\displaystyle m} 1s is equal to the binary number 0 b 0 … b n {\displaystyle 0b_{0}\dots b_{n}} minus m {\displaystyle m} . Let d c {\displaystyle d^{c}} represents the digit d {\displaystyle d} repeated c {\displaystyle c} times. The skew binary number of the form 0 c 0 21 c 1 0 b 0 … b n {\displaystyle 0^{c_{0}}21^{c_{1}}0b_{0}\dots b_{n}} with m {\displaystyle m} 1s is equal to the binary number 0 c 0 + c 1 + 2 1 b 0 … b n {\displaystyle 0^{c_{0}+c_{1}+2}1b_{0}\dots b_{n}} minus m {\displaystyle m} .
Similarly to the preceding section, the binary number b {\displaystyle b} of the form b 0 … b n {\displaystyle b_{0}\dots b_{n}} with m {\displaystyle m} 1s equals the skew binary number b 1 … b n {\displaystyle b_{1}\dots b_{n}} plus m {\displaystyle m} . Note that since addition is not defined, adding m {\displaystyle m} corresponds to incrementing the number m {\displaystyle m} times. However, m {\displaystyle m} is bounded by the logarithm of b {\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.
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.4 They were later applied to skew binomial heaps, a variant of binomial heaps that support constant-time worst-case insertion operations.5
Sloane, N. J. A. (ed.). "Sequence A169683". The On-Line Encyclopedia of Integer Sequences. OEIS Foundation. /wiki/Neil_Sloane ↩
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. S2CID 253736860. http://www.cphstl.dk/Paper/TOCS-2011/journal.pdf ↩
The Online Encyclopedia of Integer Sequences. "The canonical skew-binary numbers". https://oeis.org/A169683 ↩
Myers, Eugene W. (1983). "An applicative random-access stack". Information Processing Letters. 17 (5): 241–248. doi:10.1016/0020-0190(83)90106-0. MR 0741239. /wiki/Doi_(identifier) ↩
Brodal, Gerth Stølting; Okasaki, Chris (November 1996). "Optimal purely functional priority queues". Journal of Functional Programming. 6 (6): 839–857. doi:10.1017/s095679680000201x. https://doi.org/10.1017%2Fs095679680000201x ↩