The next few terms are: 178, 204, 212, 232, 240, 542, 558
The sequence is comprised of all the numbers which, when expressed in binary, inverting each bit results in the reverse of the original number.
For example, 52 in binary is 110100, which inveretd (ones complement) yields 001011, which is the reverse of the original value.
The following table shows each number, its binary value, and the bitwise inversion of each:
2 10 01
10 1010 0101
12 1100 0011
38 100110 011001
42 101010 010101
52 110100 001011
56 111000 000111
142 10001110 01110001
150 10010110 01101001
170 10101010 01010101
178 10110010 01001101
204 11001100 00110011
212 11010100 00101011
232 11101000 00010111
240 11110000 00001111
542 1000011110 0111100001
558 1000101110 0111010001
598 1001010110 0110101001
614 1001100110 0110011001
666 1010011010 0101100101
682 1010101010 0101010101
722 1011010010 0100101101
738 1011100010 0100011101
796 1100011100 0011100011
812 1100101100 0011010011
852 1101010100 0010101011
868 1101100100 0010011011
920 1110011000 0001100111
936 1110101000 0001010111
976 1111010000 0000101111
992 1111100000 0000011111
And just for kicks, here's the script (Javascript) that generated the above table:
var max = 1000;
for (var i = 1; i <= max; i++) {
var b = d2b(i);
if (b == bcr(b)) {
document.write(i + '\t' + b + '\t' + onescomp(b) + '\n');
}
}
function log(x, b) {
return Math.log(x)/Math.log(b);
}
function d2b(n) {
var r = '';
var cn = Math.pow(2, Math.floor(log(n,2)));
while (cn >= 1) {
if (n >= cn) {
r += '1';
n -= cn;
}
else {
r += '0';
}
cn /= 2;
}
return r;
}
function b2d(b) {
var p = 0;
var r = 0;
for (var i = b.length; i >= 0; p++) {
if (b.charAt(--i) == '1')
r += Math.pow(2, p);
}
return r;
}
function onescomp(b) {
var r = '';
for (var i = 0; i < b.length; i++) {
if (b.charAt(i) == '0')
r += '1';
else // if (b.charAt(i) == '1')
r += '0';
}
return r;
}
function reverse(x) {
var r = '';
for (var i = x.length; i >= 0; r += x.charAt(i--));
return r;
}
function bcr(x) {
return reverse(onescomp(x));
}
|