神秘常量

原文参考http://www.cnblogs.com/mq0036/p/6647383.htmlmatrix67的博文


用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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值