x&x-1

include <stdio.h>

include <stdlib.h>

include <string.h>

include

include

using std::vector;

void itoa_2(char* buffer, size_t n, unsigned int value){
assert(NULL !=buffer);
assert(n >0);

vector<unsigned char> vec_01;
unsigned char res = 0;

while(value){
	res = value%2;
	value = value/2;

	vec_01.push_back(res);
}

vector<unsigned char>::size_type size_vec_01 = vec_01.size();
assert(n -1 > size_vec_01);

for(int i = 0; i < size_vec_01; i++){
	buffer[i] = (vec_01.back() == 1 ? '1' : '0');

	vec_01.pop_back();
}

}

int main(int argc, char const argv[])
{
/
code */
int value = 9999;

char buffer[30];
memset(buffer,30,'\0');

while(value){
	//snprintf(buffer,30,"%x",value);
	itoa_2(buffer,30,value);
	printf("%s\n", buffer);

	value = value&(value-1);
}
return 0;

}

posted @ 2017-05-03 18:25  helo_blog  阅读(172)  评论(0编辑  收藏  举报