C exercise code-20151108
1. Programming implementation: How many bits are different in the binary expressions of two int (32-bit) integers m and n? Input example: 1999 2299 output example: 7
int ce_bin_wei(){//unsigned int a=1999,b=2299; int a=1999,b=2299;int i=0;int sum=0;for(i=0;i<31;i++){sum+=(a&1)^(b&1);a>>=1;b>>=1;//a/=2;//b/=2;}return sum;}
==============================================================/// Method 2
# Include <stdio. h> int count_one (int num) {int count = 0; while (num) {count ++; num = num & (num-1 ); // change the first 1 in binary from the occurrence bit to zero} return count;} int main () {int n1 = 0; int n2 = 0; scanf ("% d", & n1, & n2); int num = n1 ^ n2; int ret = count_one (num); printf ("% d \ n ", ret); system ("pause"); return 0 ;}
2. Compile the function: 1 unsigned int reverse_bit (unsigned int value); the value returned by this function after the binary mode of value is flipped from left to right. For example, on a 32-bit machine, the value 25 contains the following: 00000000000000000000000000011001 after the flip: (2550136832) the result of the 10011000000000000000000000000000 program is returned: 2550136832
unsigned int reverse_bit(unsigned int value){unsigned int rev_val=0;int i=0;for(i=0;i<32;i++){rev_val<<=1;rev_val+=(value&1);value>>=1;}return rev_val;}
========================================================== ===== Another
typedef unsigned int uint;uint reverse_bit(uint value){int i = 0;uint sum = 0;for (i = 0; i < 32; i++){sum += ((value >> i) & 1)*pow(2, 31 - i);}return sum;}
========================================================== ======= 3. do not use (a + B)/2 to calculate the average value of two numbers.
# Include <stdio. h> int main () {int num1 = 10; int num2 = 20; int avg = num1 & num2 + (num1 ^ num2)> 1; // method 1 num1 & num2
// ① First let's talk about the same bit ====== the same bit in the num1 and num2 binary to form two equal numbers. a1 a2 the two equal numbers are added and divided by 2 equals to their own bodies. // that is, num1 & num2 (such as num1 = (1) in decimal = (0001), it is simply represented here. No 32-bit num2 = (3) decimal = (0011) binary is written) // num1 & num2 = 0001 (0001 = (0001 (equivalent to num2 in num1) + 0001)/2) // ② let's talk about different bits =====( num1 ^ num2). The two bits are different or (No overflow occurs) the one-digit shift on the right is equivalent to no overflow in the two steps except the two. // method 2 does not overflow either.
//=====================int avg2 = num1 - (num1 - num2) / 2;===================printf("%d\n", avg);system("pause");return 0;}
4. Only one number appears in a group of data. All other numbers are paired. Find this number. (Bitwise Operation)
int find_d(int arr[],int len){int i=0;int temp=0;for(i=0;i<len;i++){temp^=arr[i];}return temp;}
// ================================= The same two numbers are different or the result is 0 ================== === a group of numbers is also equivalent to removing the same final result, where different numbers are exclusive or result 5. [enhanced] an integer array contains only two numbers, the other numbers appear twice. Write a program to find the numbers that appear only once. [The nature of the exclusive or operation: Any number exclusive or it is equal to 0 by itself] [with the solution of simple problem 4 above, we will return to the original problem. If you can divide the original array into two subarrays. Each sub-array contains a number that appears only once, and other numbers appear twice. If the original array can be split in this way, the preceding method is to find the numbers that appear only once. The result is that two numbers appear only once. Because the other numbers appear twice, they are all offset in the XOR or. Because these two numbers are certainly different, the difference or result is certainly not 0, that is, at least one of the binary representation of the result number is 1. In the result number, we locate the first digit as the nth digit. Now we divide the numbers in the original array into two subarrays based on whether the nth bit is 1. the nth bit of each number in the first subarray is 1, the Nth digit of each number in the second sub-array is 0. (The two numbers are different, which means that the bit of 1 is different.)]
# Include <stdio. h> // # include <stdlib. h> int main () {int arr [] = {,-}; int I = 0; int result = 0; int num1 = 0; int num2 = 0; int count = 0; int len = sizeof (arr)/sizeof (arr [0]); for (I = 0; I <len; I ++) {result ^ = arr [I]; // returns two results with an exclusive or ending number} // returns the first distinct position of the two numbers. count = 1; I = 1; while (result) {if (result & I) = 1) {break;} result >>= 1; count <=1 ;} // divide the array into two groups: num1 = 0; num2 = 0; for (I = 0; I <len; I ++) {if (arr [I] & count) = 0) {num1 ^ = arr [I];} else {num2 ^ = arr [I];} printf ("% d \ n", num1, num2); // system ("pause"); return 0 ;}