x&x-1

include <stdio.h>

include <stdlib.h>

include <string.h>

include

include

using std::vector;code

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

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();
}

}it

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

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;

}gc

本站公眾號
   歡迎關注本站公眾號,獲取更多信息