int ret = 0, flag = 0; ret |= (1 << (a - 1)); ret |= (1 << (b - 1)); ret |= (1 << (c - 1)); ret |= (1 << (d - 1)); ret |= (1 << (e - 1)); while (ret) { if (0 == ret % 2) { flag = 1; } ret / = 2; } if (0 == flag) { printf("比賽的名次為:\n"); printf("a=%d\nb=%d\nc=%d\nd=%d\ne=%d\n", a, b, c, d, e); } 四、尋找兩個int(32位)整數m和n的二進制表達中,有多少個位(bit)不同 int different_bits(int num_1, int num_2) { int count = 0; for (int i = 0; i < 32; i++) { if (((num_1 >> i) & 1) ^ ((num_2 >> i) & 1)) { count++; } } return (count); }
五、將一個無符號數的二進制數逆置 unsigned int reverse_bit(unsigned int num) { int i = 0; unsigned int ret = 0; for (i = 0; i < 32; i++) { ret<<=1; ret |= ((num >> i) & 1); } return (ret); } 六、求兩個數的平均值,無溢出 int average(int a, int b) { return((a & b) + ((a ^ b) >> 1)); } 七、找出一組數據中單獨出現的數字 int find_only(int arr[], int size) { for (int i = 1; i < size; i++) { arr[0] ^= arr[i]; } return (arr[0]); }