原文参考http://www.cnblogs.com/mq0036/p/6647383.html 和matrix67的博文
用c++写了下,获得了其中一个“神秘常量” hex: 0x04653adf bin:00000100 01100101 00111010 11011111 注意神秘常量的前4位一定要是0
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef unsigned int uint_32;
class DeBruijn
{
public:
uint_32 newNum;
string newStr;
uint_32 newArr[32];
list<string> l;
DeBruijn(){}
~DeBruijn() {}
/*
uint_32 exist = 0x077CB531;
uint_32 exist_arr[32] = { 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 };
uint_32 getBitPos_exist(int v) {
return exist_arr[((uint_32)((v & -v) * exist)) >> 27];
}
*/
uint_32 getBitPos_new(int v) {
return newArr[((uint_32)((v & -v) * newNum)) >> 27];
}
void getnew(string str) {
string tmp_str = str.substr(1, 4);
string tmp_str0 = tmp_str + "0";
string tmp_str1 = tmp_str + "1";
if (l.size() == 32) {
cout << "ok" << endl;
getNum_new();
getArr_new();
return;
}
if (find(l.begin(), l.end(), tmp_str0) == l.end() && l.size() != 32) {
l.push_back(tmp_str0);
getnew(tmp_str0);
if (l.size() == 32)
return;
l.pop_back();
}
if (find(l.begin(), l.end(), tmp_str1) == l.end() && l.size() != 32) {
l.push_back(tmp_str1);
getnew(tmp_str1);
if (l.size() == 32)
return;
l.pop_back();
}
}
void getNum_new() {
string res_str = l.front();
uint_32 res_uint;
list<string>::iterator index = l.begin();
index++;
for (int n = 1; n <= 27; n++) {
res_str += (*index).substr(4, 1);
index++;
}
newStr = res_str;
res_uint = strtol(res_str.c_str(), NULL, 2);
newNum = res_uint;
}
void getArr_new() {
for (int i = 0; i < 32; i++) {
int index = ((uint_32)(newNum << i)) >> 27;
newArr[index] = i;
}
}
};
int main()
{
DeBruijn db = DeBruijn();
// uint_32 res = db.getBitPos(123456);
// cout << res << endl;
db.l.push_back("00000");
db.getnew("00000");
cout << "bin: " << db.newStr << endl;
cout << hex << "hex: " << db.newNum << endl;
// for (list<string>::iterator i = db.l.begin(); i != db.l.end(); i++)
// cout <<(*i) << endl;
uint_32 res = db.getBitPos_new(123456);
cout << "the zero number of suffix: " << res << endl;
return 0;
}