OJ Essay--"1102-Hamming distance"--bitwise operation

Source: Internet
Author: User

The topics are as follows:

Description
The Hamming distance is the number of bits in the binary case where an integer becomes another integer that needs to be flipped. For example 2 conversion to 3 requires flipping 1 bits, so the Hamming distance from 2 to 3 is 1. Give you two positive integers x and y, (x,y<=1,000,000,000) to ask for their hamming distance. The first line of input is an integer n, which indicates the number of samples. After each line two integers x and y. Output one integer per line, and the result of the corresponding sample.
Sample Input
21 24 7
Sample Output
22

Analysis Ideas:

In a nutshell, a variety of input, for each set of inputs are converted to binary and then the logical XOR operation, that is, to compare the two binary numbers corresponding to each bit, the same is 0 or the same as 1 returns 0, a 1 is 1 returns 1. For example, a binary representation of 2 is ... 00000010,3 binary is represented as ... 00000011, the result of their XOR operation is ... 00000001, that is, the last a different need to "flip". So Hamming distance is actually to see two number of binary XOR operation after 1 of the number of bits.

Core issues:

With the above idea, the question now is how to convert the input decimal number into binary for the XOR operation? Start thinking is to convert the decimal number through modulo operations into binary numbers stored in the array, and then use the loop to compare each bit, two number on that one on the number of the difference is counted +1.

It was later recalled that the C language provides the "bitwise operator", which can be used to a^b the binary form of the two decimal numbers (without having to convert the system itself) directly. For details, please refer to "http://blog.csdn.net/chen825919148/article/details/8049069"

Operator Role
& Bitwise logic WITH: Bitwise AND operator "&" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase. Only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion.
For example: The 9&5 can be written as follows: 00001001 (9 twos complement) &00000101 (5 's twos complement) 00000001 (1 of the twos complement) is visible 9&5=1.
| Bitwise logic OR: Bitwise OR operator "|" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase or. As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. The two numbers participating in the operation are in complement.
For example: 9|5 can be written as follows: 00001001|00000101=00001101 (decimal 13) is visible 9|5=13
^ Bit logic XOR: Bitwise XOR OR operator "^" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary dissimilarity or, when the two corresponding binary differences, the result is 1. Participation in the arithmetic is still in complement, for example 9^5 can be written as follows: 00001001^00000101=00001100 (decimal 12)
~ Bitwise logic Inverse: negation operator ~ is a single-mesh operator with right-associative. Its function is to reverse the bitwise negation of the number of participating operations. For example, the operation of the: ~ (0000000000001001) result is: 1111111111110110
>> Move right: The right-shift operator ">>" is the binocular operator. The function is to shift all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move. For example: Set a=15,a>>2 to move 000001111 right to 00000011 (decimal 3). It should be stated that, for the signed number, when moving right, the symbol bit is moved along with it. When positive, the highest bit is 0, while negative, the sign bit is 1, the highest bit is 0 or the complement 1 depends on the requirements of the compilation system.
<< Move left: The left shift operator << is the binocular operator. Its function to the left of the "<<" operand of all the binary all left a number of bits, the "<<" to the right of the number to specify the number of bits moved, high drop, low 0. For example: A<<4 refers to moving the binary of a to the left by 4 bits. such as a=00000011 (decimal 3), 00110000 (decimal 48) After moving left 4 bits.

Finally attached to my program source code:

1#include <stdio.h>2 intMain ()3 {4     intn,i,a,b;5scanf"%d",&n);6      for(i=1; i<=n;i++)7     {8scanf"%d%d",&a,&b);9A^=b;//The XOR operation of A, B, the result of the operation is stored in a;Tenb=0;//Reset B to 0 for counting; One          while(a) A         { -             if(a&1)//Binary A in (binary) ... 00001 and the operation, the same 1 is 1, the remaining 0; that is, the result of the XOR operation the last one is 1 o'clock Count +1 -b++; thea>>=1;//For a right shift operation, remove the lowest bit, the next cycle compared to a bit, until the value of a is 0; -         } -printf"%d\n", b);//The number of digits after output XOR operation is 1, that is, the Hamming distance of two numbers; -     } +     return 0; -}

OJ Essay--"1102-Hamming distance"--bitwise operation

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.