?? bitcount.c
字號:
#include <stdio.h>/* * function bit_count * Counts the number of bits on (i.e., = 1) in an unsigned int * (This code assumes that an int is 32-bits!) * Parameter: * x - number to count bits of * Return value: * number of bits on in x */int bit_count(unsigned int x ){ static unsigned int mask[] = { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF }; int i; int shift; /* number of positions to shift to right */ for( i=0, shift=1; i < 5; i++, shift *= 2 ) x = (x & mask[i]) + ( (x >> shift) & mask[i] ); return x;}int main(){ unsigned int x; printf("Enter a number: "); scanf("%i", &x); printf("There are %d bits on in %X\n", bit_count(x), x); return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -