Blue Bridge Cup - Chapter 1 - bit computing

This is an article about bit operation, found-bit computing in the role of the learning process is wonderful, is not easy to observe some things out, but it is very easy to use bit computing solutions

**

1. The number in binary form to obtain a decimal number of 1

**

Ideas: 1 & 1 principle is true for the rest of the big false principle when using the & operator.
Tested once every right to make this move a number, and then do 1 & operation, if this explanation is a bit above this number is 1, then the counter is incremented, otherwise the cycle continues until the right number becomes 0 The code to achieve as

    #include <stdio.h>
    int main()
    {
     int n = 0, num = 0, t = 0;
     printf("请输入一个数字,然后会输出它的二进制里有几个1\nn = ");
     scanf("%d",&n);
     t = n;
     while(n != 0)
     {
      if(n & 1 == 1)
      num++;
n= n>>1;
     }
     printf("%d里面有%d个1\n",t,num);
     return 0;
    }

2. Find a unique number from the number appears twice in the known range.

Example: There are 1001 number range is 0-999 number is only a number of the other two occurrences were only appears once. Obtain this number.

Ideas:

Bit operation XOR operation which can eliminate a bunch of numbers in even number appears. I.e., x ^ a ^ a = x.

And because the scope of given numbers, so the problem can evolve into the range of the number of all even appear as compared to the target number appears odd number of times. I.e., the first variable x 0 XOR operation with all numbers 0-999 is a. This step is to ensure that all the numbers appear once.

This then all numbers x and XOR operations. This time you can eliminate the presence of a number of reservations and the number appears twice in the next.

(The number of colonies to identify a single direct output to the exclusive OR operation)

code show as below:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

void show(int a[], int N);

int main()

{

    int N = 11, x = 0, number = 0;

    int *a;

    a = (int *)calloc(N,sizeof(int));

    int i = 0;

    for(i = 0; i<N-1; i++)

        a[i] = i+1;

    srand(time(NULL));

    //show(a,N);

    number = rand() % (N-1)+1;

    a[N-1] = number;

    show(a,N);

    x= 0;

    for(i = 1; i <= N-1; i++)

        x = x^i;

    for(i = 0; i< N; i++)

        x = x^a[i];

    printf("%d\n",x);

    return 0;

}

void show(int a[], int N)

{

    int i = 0;

    for(i = 0; i < N; i++)

        printf("%d%c",a[i],i == N-1?'\n':' ');

}

3. Analyzing a number is not an integer power of 2

Ideas: Write an integer power of 2 can be found, a power of 2 are at the highest position is 1, and 0.

After a re-write it.

For example, 8 and 7

Binary 8 1000

7 is binary 0111.

Another example 16 and 15

16 10000 Binary

Binary 15 is 01111

Can be found in the number of integer power of 2 and its minus a few do & operation, the result is always zero.

You can use this feature to find the integer power of two, the following code to achieve.

//以0为测试结束的标志 

#include <stdio.h>

int main()

{

    int n = 0;

    printf("请输入一个数,然后会输出它是不是2的整数次幂\nn
= ");

    while(scanf("%d",&n)!=EOF)

    {

        if(n == 0)

        break;

        if((n & (n-1) )== 0)

        printf("YES\n");

        else 

        printf("NO\n");

        printf("请输入一个数,然后会输出它是不是2的整数次幂\nn
= ");

    }

    return 0; 

}

4. The parity bit integer swap

Thinking: & calculation or the use of, as the data type of int 32-bit number only,

So as long as

Using a all even bit is 1 and the odd bit is 0 the count and the number of do & operator can obtain the data on the even bit, the same odd bit requires only one all odd bit is 1 and the even bit number 0 and the number of operations you can do & get the odd bit of this number.

And finally the right one even-odd left one can be.

Code to achieve the following

#include <stdio.h>

int main()

{

 int i = 0,a=0;

int ou=0,ji=0;

printf("请输入一个数将输出它的奇数位和偶数位调换前后的数\ni = "); 

scanf("%d",&i);

ou = i&0xaaaaaaaa;

ji = i&0x55555555;

a = ou ^ ji;

printf("奇偶转换之前 = %d\n",a);

a = (ou>>1)^(ji<<1);

printf("奇偶转换过后 = %d\n",a);

}

The move by two integer, floating-point binary representation of an integer.

Code to achieve the following

#include <stdio.h>

#define max 34

int main()

{

    double n = 0, t = 0;

    int i = 0, num = 0;

    int a[max]= {0};

    printf("请输入一个实数\nn = ");

    scanf("%lf",&n);

    t = n;

    //乘二挪整

    while( t )

    {

        t*=2;

        if(t >= 1)

        {

           a[i] = 1;

           t -= 1;

        }

        else

           a[i] = 0;

        i++;

    }

    num = i-1;

    //printf("num = %d\n",num);

    if(num > 32)

        printf("ERROR\n");

    else

    {

        printf("0.");

        for(i = 0; i <= num; i++)

           printf("%d",a[i]);

    }

    return 0;

}

Guess you like

Origin blog.csdn.net/CSDNsabo/article/details/91476769