算法笔记-0302

需要研究:

#include <iostream>
using namespace std;

int main(){
	int a = 1, b = 32;
	cout << (a << b) << endl;
	cout << (1 << 32) << endl;

	system("pause");
	return 0;
}

 

 

 

算一个数的二进制里有多少个1: x&(x-1)

#include <iostream>
using namespace std;
int func(int x)
{
	int countx = 0;
	while (x)
	{

		countx++;
		x = x&(x - 1);
	}
	return countx;
}
int main(){
	int i;
	i = func(65530);
	cout << i;
	system("pause");
	return 0;
}

 

 

二进制数 第n位是0还是1?

#include <iostream>
using namespace std;
unsigned int func(int num, int indexBit)
{
	num = num >> indexBit;
	return num&1;
}
int main(){
	int i;
	i = func(65530,0);
	cout << i;
	system("pause");
	return 0;
}

  

  

 

posted @ 2016-03-02 11:59  wujixing909  阅读(167)  评论(0编辑  收藏  举报