Millet Pen Test

Source: Internet
Author: User

Title: There are 10 kinds of people in the world, one understands the binary system, one does not understand. So you know the binary representation of two int32 integers m and n, how many bits are different?

Input Example:
1999 2299
Output Example:
7

Background: Convert two binary numbers to equal lengths (shorter ones, 0), then two binary digits to bitwise XOR, and 1 of those bits in the result, that is, the two binary digits are different bits.
        The data in the computer is saved and the operation is in the form of binary, but not directly the original code, but the original code of the complement, that is, in the computer, the data in the operation of data are in the complement.
XOR:
XOR (XOR) is a math operator. It is applied to logical operations. The mathematical symbol of XOR is "⊙" and the computer symbol is "XOR". The algorithm is: a⊕b = (¬a∧b) ∨ (a∧¬b) If a, b two values are different, then the XOR result is 1.                If the A and B two values are the same, the XOR result is 0. The number of symbols in a computer has three representations, that is, the original code, the inverse code and the complement.       The three representations have both symbolic and numeric digits, and the sign bit is 0 for "positive", 1 for "negative" and the value bit, and three for the method of representation.      1, a negative integer (or the original code) and its complement (or complement) added, and for the module.      2, the complement of an integer and then seek complement, equal to the integer itself.                  3, complement positive zero and negative 0 means the same method. The complement of a positive integer is the second binary representation, the same as the original code to find a negative integer complement, the corresponding positive binary representation of all the bit negation (including the sign bit, 0 change to 0) plus 1
Specific code:
1#include <stdio.h>2#include <stdlib.h>3 4 intMain ()5 {6     intn =1999;7     intm =2299;8     intCompare1 =0, Compare2 =0;9     intresult =0, Count =0;Ten  One     if(M >=0 ) ACompare1 =m; -     Else -Compare1 = (~m) +1;//if it is negative, the complement is the source code the  -     if(N >=0 ) -Compare2 =N; -     Else +Compare2 = (~n) +1; -  +result = Compare1 ^ compare2;//Bitwise XOR or A     //printf ("%d\n", result); at  -      while(Result! =0){ -  -         if(result%2==1) count++;//The calculation has several 1 -result = result/2; -  in     } -     returncount; to}

Millet Pen Test

Related Article

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.