The skew binary number system is a non-standard positional numeral system in which the nth digit contributes a value of
2n+1-1
2n
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 |
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(2n+1-1)+1=2n+2-1
Other arithmetic operations may be performed by switching between the skew binary representation and the binary representation.[1]
To convert from decimal to skew binary number, one can use following formula:[2]
Base case:
a(0)=0
Induction case:
a(2n-1+i)=a(i)+10n-1
Boundaries:
0\lei\le2n-1,n\ge1
To convert from skew binary number to decimal, one can use definition of skew binary number:
S=
N | |
\sum | |
i=0 |
i+1 | |
b | |
i(2 |
-1)
bi\in{0,1,2}
blsb
using namespace std;
long dp[10000];
//Using formula a(0) = 0; for n >= 1, a(2^n-1+i) = a(i) + 10^(n-1) for 0 <= i <= 2^n-1,//taken from The On-Line Encyclopedia of Integer Sequences (https://oeis.org/A169683)
long convertToSkewbinary(long decimal)int main
using namespace std;
// Decimal = (0|1|2)*(2^N+1 -1) + (0|1|2)*(2^(N-1)+1 -1) + ... // + (0|1|2)*(2^(1+1) -1) + (0|1|2)*(2^(0+1) -1)//// Expected input: A positive integer/long where digits are 0,1 or 2, s.t only least significant bit/digit is 2.//long convertToDecimal(long skewBinary)int main
Given a skew binary number, its value can be computed by a loop, computing the successive values of
2n+1-1
n
n
The skew binary number of the form
b0...bn
m
0b0...bn
m
dc
d
c
c0 | |
0 |
c1 | |
21 |
0b0...bn
m
c0+c1+2 | |
0 |
1b0...bn
m
Similarly to the preceding section, the binary number
b
b0...bn
m
b1...bn
m
m
m
m
b
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.[3] They were later applied to skew binomial heaps, a variant of binomial heaps that support constant-time worst-case insertion operations.[4]