A summary of the topic involved in the written interview (i) _ bit operation

Source: Internet
Author: User
Tags bitwise
Topic One: Topic description

There is a positive integer, please find the binary representation of 1 of the same number, and the closest size of the two number. (one slightly larger, one slightly smaller)

Given a positive integer int x, return a vector that represents the two number (small in front) of the request. Guarantee that the answer exists. Test Sample:

2
Back: [1,4] Topic Analysis: For this topic, I think the most important thing is to find a number of binary representation of 1 of the number. There are many ways to calculate the number of 1 in a binary representation of a number: Method 1: With 1 for &, and then the number of 1 in 32 bits. (less efficient, no matter how large the number, must be cycled 32 times) Method 2: Parallel algorithm: The adjacent bit sum, repeat the process, until the last only one bit, is the number of binary representation of 1 of the number. Method 3: Fast method, any number and smaller than 1 of the number do & operations, the result will be less than the original number of 1, which can also be counted 1 of the number. Since this is a faster method, this approach is used to find the number 1. Attention

Many beginners, for this problem will also think of the method of module, but in addition to this approach, the processing of positive numbers when there is no problem, but when dealing with negative, it is wrong, the following gives the test code:

void Test ()
{
       int x =-1;
       int count = 0;
       while (x)
       {
              if (x 2!= 0)
                     ++count;
              x/= 2;
       }
       cout << Count << Endl;
}

In fact, the binary representation of 1 contains 32 1. So, this approach is wrong.
Well, having said so much, the point is to solve the problem. Below gives the implementation code of the subject:
Implementation code:
Class Closenumber {public
:
    int count (int x)
    {
        int count = 0;
        while (x)
        {
            ++count;
            x = x & (x-1);
        }
        return count;
    }
    vector<int> getclosenumber (int x) {
        //write code here
        int countone = Count (x);
        vector<int> ret;
        for (int i = x-1 i > 0; i)
        {
            if (Count (i) = = Countone)
            {
                ret.push_back (i);
                break;
            }   
        }
        for (int i = x + 1;; ++i)
        {
            if (Count (i) = = Countone)
            {
                ret.push_back (i);
                break;
            }
        }
        return ret;
    }
;

Topic Two: Topic description

Write a program to exchange a binary number of odd digits and even digits. (The less you use the command the better)

Given an int x, return the number int after the exchange. Test Sample:

10
Return: 5

Topic Analysis: If we can get a number of odd digits and even digits of the value, and then exchange it. It is simpler to get the value of an odd digit and the value of an even digit. The value of an odd digit: the value of a given number of &0xaaaaaaaa digits: A method for giving a digital &0x55555555 Exchange: The value of an odd digit is shifted to the right, and the value of an even digit is shifted to the left one, and the result is added.
Code implementation:
Class Exchange {public
:
    int Exchangeoddeven (int x) {
        //write code here
        int odd = (x & 0xAAAAAAAA);/ In exchange for x's odd-digit information
        int even = (x & 0x55555555);//Even digit information return
        (odd >> 1) + (even << 1);
    }
;

Topic Three: Topic description

There is a real number between 0 and 1, and the type is double, which returns its binary representation. Returns "Error" if the number cannot be accurately expressed in 32-bit binary notation.

Given a double of 0 to 1 real numbers, return a string that represents the binary representation of the number or "Error." Test Sample:

0.625
return: 0.101

Topic Analysis: This topic examines decimal is less than 1 positive decimal converts to binary number method, this has studied the computer foundation the person all transforms, is even multiplication, here does not elaborate. It is particularly noteworthy that the method of comparing floating point number with 0, this problem, the front of the article is summarized. That is, the number is considered to be 0, between a positive and a negative decimal that is infinitely close to 0. Please see the representation method in the code below.
Code implementation:
Class Bindecimal {public
:
#define EXP POW (10,-7)
    string printbin (double num) {
        //write code
        here string ret;
        if (Num >= 1) return
            ret;
        int count = 0;
        Ret.push_back (' 0 ');
        Ret.push_back ('. ');
        while (!) ( num >-exp && num < exp))
        {
            num = num * 2;
            if (Num >= 1.0)
            {
                ++count;
                Ret.push_back (' 1 ');
                num-= 1.0;
            }   
            else
            {
                ++count;
                Ret.push_back (' 0 ');
            }
            if (count = =) {return
              "Error";
            }

        }
        return ret;
    }
;

"Summary" 1. A method of comparing floating-point numbers with 0 is not directly comparable. 2. Decimal decimal conversion to binary decimal method-----multiplication. 3. The number of 1 in a binary representation of a number. 4. How to get a number of odd digits and even digits corresponding to the value---the number and 0xAAAAAAAA bitwise with, and 0x55555555 bitwise with.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.