コード例 #1
0
ファイル: bits.c プロジェクト: tectrendz/p
int main1()
{
    int i = 0xf;
    printf("Bit Operation \n");
    printf("Number of ones in 0x24 is %d \n", count1s(0x24));
    printf("Number of ones in 0x24 is %d \n", count1s1(0x24));
    printf("Number of zeros in 0x24 is %d \n", count0s1(0x24));
    printf("Number  %x and ~number is %x ",i, ~(i)); 
    return 1;
}
コード例 #2
0
ファイル: question5_3_v1.c プロジェクト: dilrajssokhi/ctci
int count1s(unsigned int num) {
	int count;
	if (num == 0) {
		count = 0;
		return count;
	}
	else {
		count = count1s(num/2);
		if (num % 2 == 1) 
			count++;
		return count;
	}
}
コード例 #3
0
ファイル: Util.hpp プロジェクト: weristdas/GRE
	void BitMap2Set(unsigned long* bitmap, const long size, std::vector<T>& keys){
		const long nKeys = count1s(bitmap, size);
		keys.resize(nKeys);
		long n = 0;
		for(long i = 0; i < size; i++){
			T key = i*sizeof(unsigned long);
			unsigned long tmp = bitmap[i];
			while(tmp){
				if(tmp & 0x1UL)
					keys[n++] = key;
				tmp >>=1;
				key++;
			}
		}
		//assert(n == nKeys);
	}
コード例 #4
0
ファイル: init.cpp プロジェクト: ixixi/apery
// square の位置の rook, bishop それぞれのMagic Bitboard に使用するマジックナンバーを見つける。
// isBishop  : true なら bishop, false なら rook のマジックナンバーを見つける。
u64 findMagic(const Square square, const bool isBishop) {
	Bitboard occupied[1<<14];
	Bitboard attack[1<<14];
	Bitboard attackUsed[1<<14];
	Bitboard mask = (isBishop ? bishopBlockMaskCalc(square) : rookBlockMaskCalc(square));
	int num1s = (isBishop ? BishopBlockBits[square] : RookBlockBits[square]);

	// n bit の全ての数字 (利きのあるマスの全ての 0 or 1 の組み合わせ)
	for (int i = 0; i < (1 << num1s); ++i) {
		occupied[i] = indexToOccupied(i, num1s, mask);
		attack[i] = attackCalc(square, occupied[i], isBishop);
	}

	for (u64 k = 0; k < UINT64_C(100000000); ++k) {
		const u64 magic = g_mt64bit.randomFewBits();
		bool fail = false;

		// これは無くても良いけど、少しマジックナンバーが見つかるのが早くなるはず。
		if (count1s((mask.merge() * magic) & UINT64_C(0xfff0000000000000)) < 6)
			continue;

		std::fill(std::begin(attackUsed), std::end(attackUsed), allZeroBB());

		for (int i = 0; !fail && i < (1 << num1s); ++i) {
			const int shiftBits = (isBishop ? BishopShiftBits[square] : RookShiftBits[square]);
			const u64 index = occupiedToIndex(occupied[i], magic, shiftBits);
			if      (attackUsed[index] == allZeroBB())
				attackUsed[index] = attack[i];
			else if (attackUsed[index] != attack[i])
				fail = true;
		}
		if (!fail)
			return magic;
	}

	std::cout << "/***Failed***/\t";
	return 0;
}
コード例 #5
0
ファイル: ShareMemory.c プロジェクト: elvis460/ShareMemory
int B() {
    const int SIZE = 4096;
    const char *name = "OS";
    int shm_fd;
    void *ptr;
    int i,result;
    static int binary[1000];
    static int ten[1000];
    static int  count[1000];
    static int index=0;
    // read from SM and convert to bunary
    shm_fd = shm_open(name, O_RDONLY,0666);
    if(shm_fd == -1){
        printf("shared memory failed\n");
        exit(-1);
    }
    
    
    ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
    if(ptr == MAP_FAILED){
        printf("Map Failed\n");
        exit(-1);
    }
    
    
    i = atoi(ptr);
    
    if(i==-1){ //取最大輸出
        int max = 0;
        for(int b=0;b<index;b++){
            if(count[b]>max){
                max=count[b];
            }
        }
        shm_fd = shm_open(name, O_CREAT|O_RDWR,0666);
        
        
        ftruncate(shm_fd, SIZE);
        ptr = mmap(0, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, shm_fd, 0);
        if(ptr == MAP_FAILED){
            printf("Map Failed\n");
            return -1;
        }
        
        for(int c=0;c<index;c++){
            if(count[c]==max){
                sprintf(ptr,"%d:%d;",ten[c],binary[c]);
                ptr+=strlen(ptr);
            }
        }
        
    }else if(i==-2){  //取最小輸出
        int min=count[0];
        for(int j=1;j<index-1;j++){
            if(min>count[j]){
                min=count[j];
            }
        }
        
        shm_fd = shm_open(name, O_CREAT|O_RDWR,0666);
        
        ftruncate(shm_fd, SIZE);
        ptr = mmap(0, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, shm_fd, 0);
        if(ptr == MAP_FAILED){
            printf("Map Failed\n");
            return -1;
        }
        for(int g=0;g<index-1;g++){
            if(count[g]==min){
                sprintf(ptr,"%d:%d;",ten[g],binary[g]);
                ptr+=strlen(ptr);
            }
        }
        
    }
    else{
        
        result= int_to_binary(i);
        
        
        
        //write result back to SM
        
        shm_fd = shm_open(name, O_CREAT|O_RDWR,0666);
        
        
        ftruncate(shm_fd, SIZE);
        ptr = mmap(0, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, shm_fd, 0);
        if(ptr == MAP_FAILED){
            printf("Map Failed\n");
            return -1;
        }
        
        
        sprintf(ptr,"%d:%d;",i,result);
        
        //store all int and binary
        ten[index]=i;
        binary[index]=result;
        
        
        
        //store the binary to string (for compare later)
        
        count[index] = count1s(i);
        
        
        index++;
        
    }
    
    
    
    return 0;
}
コード例 #6
0
ファイル: question5_3_v1.c プロジェクト: dilrajssokhi/ctci
int main(int argc, char *argv[]) {
	/*
	printf("Argument count: %d\n", argc);
	for (int i = 0; i < argc; i++) {
		printf("Argument vector values:%s at %p memory\n", argv[i], argv[i]);
		for (char *j=argv[i]; *j!='\0'; j++) {
			printf("Another way to print argument vector values: "
                               "%c at %p memory\n", *j, j);
		}
	}

	unsigned int num;

	num = pow(2,31);
	printf("Number : %u\n",num);

	num = 1<<31;
	printf("Number : %u\n",num);
	printf("Number(in hex): %x\n",num);
	printBits(num);printf("\n");

	num = pow(2,32)-1;
	printf("Number : %u\n",num);

	num = 0;
	for (int len = 0; len <32; len++) {
		num = num + pow(2,len);
	}
	printf("Number : %u\n",num);

	num = 0;
	for (int len = 0; len <32; len++) {
		num = num + (1<<len);
	}
	printf("Number : %u\n",num);
	printf("Number(in hex): %x\n",num);
	printBits(num);printf("\n");

	num = 0;
	for (int len = 3; len <32; len=len+4) {
		num = num + (1<<len);
	}
	printf("Number : %u\n",num);
	printf("Number(in hex): %x\n",num);
	printBits(num);printf("\n");
	*/

        unsigned int num = 0;
        num = num + (1<<31) + (1<<28) + (1<< 24) + (1<< 20) + (1<<15) + (1<<10) + (1<<5) + (1<<3) + (1<<1) + (1<<0);
	printf("Num: %u\n",num);
        printBits(num);printf("\n");

	int num1s = 0;
	num1s = count1s(num);
	printf("Number of 1's: %d\n",num1s);

	unsigned int newnum = num;
	int newnum1s = num1s;
	do {
		newnum = newnum + 1;
		newnum1s = count1s(newnum);
	}
	while (newnum1s != num1s);

	printf("Num larger than %u with same 1s: %u\n",num, newnum);
        printBits(newnum);printf("\n");
	printf("Number of 1's: %d\n",newnum1s);

	newnum = num;
	do {
		newnum = newnum - 1;
		newnum1s = count1s(newnum);
	}
	while (newnum1s != num1s);

	printf("Num smaller than %u with same 1s: %u\n",num,newnum);
        printBits(newnum);printf("\n");
	printf("Number of 1's: %d\n",newnum1s);

	return 0;
}