Bit arithmetic problem

Source: Internet
Author: User

Bit arithmetic

A bitwise operation is a binary representation of a number, a 0 or a 1 operation on each.

The first step in understanding bit arithmetic is to understand the binary. Binary means that each digit of the number is 0 or 1. For example, decimal 2 is converted to binary after 10. In the programmer's circle there is a long-circulated joke that there are 10 kinds of people in the world, one knows the binary, and the other does not know the binary ...

In fact, the binary operation is not difficult to master, because the bitwise operation of a total of only 5 operations: with, OR, XOR, left shift, right shift. As the following table:

and (&) 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1
or (|) 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1
XOR (^) 0 ^ 0 = 0 1 ^ 0 = 1 0 ^ 1 = 1 1 ^ 1 = 0

left shift Operation :

The left shift operator M<<n indicates that m shifts n bits to the left. When the n bit is shifted to the left, the leftmost n bits are discarded, with n 0 on the far right. For example:

0010100001010000

right-shift operation :

The right-shift operator m>>n means to shift m to the right N-bit. When you move the n-bit right, the rightmost n bit is discarded. But it's a little more complicated to handle the leftmost bit when you move right. It is important to note that if the number is an unsigned value , the left-most n-bit is filled with 0. If the number is a signed value , the left-most n-bit is filled with the sign bit of the number. That is, if the number was originally a positive, then the left side of the leftmost complement N 0, if the number was originally negative, then the left side after the right to fill N 1. The following is an example of a heap of two 8-bit signed numbers for right shift:

0000001011110001

Add:

Right-shift Operation X>>k's behavior is a bit tricky. In general, and its support for two forms of right shift: logical right SHIFT and arithmetic right shift. The logical right moves at the left to complement K 0, and the arithmetic right is the value that complements K's most significant bit at the left.

The C language standard does not explicitly define which kind of right-shift should be used. For unsigned data (that is, integer objects declared with qualifier unsigned), the right shift must be logical. For signed data (the default declared integer object), the right shift of arithmetic or logic is possible. However, this also means that any code that assumes one or another right-shifted form has a potential portability problem. In fact, almost all compiler/machine combinations use arithmetic right shifts for signed data , and we generally assume that the machine will use this right shift (arithmetic right shift).

There is an equivalence relationship between the operations of shifts: moving integers to the right and dividing integers by 2 is mathematically equivalent.

a left shift is equivalent to a = a * 2; //a left 2 bit equivalent to a = a * 2 of 2 times (4);  

The internal computer only recognizes 1, 0, and decimal needs to be binary in order to use the shift operator <<,>>.

81;cout<<p<<endl; 

Here, 8 left one is the result of 8*2 16.

  The shift operation is one of the most efficient operations to compute multiplication/divide .

Bitwise AND (&) Its function is to participate in the operation of the two numbers corresponding to the bits phase. Only the corresponding two bits are 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion.

Let me give you an example as follows:

Title: Please implement a function, enter a positive number, output the binary representation of 1 of the numbers.

 1 int count (BYTE N ) 2 {3 int num = 0;while (n) {5 N &= (n -1); 6 num++;}8 return num; 9}             

Here's a point of knowledge: Subtracting an integer by 1, and then doing and operating with the original integer will change the rightmost 1 of the integer to 0. So how many 1 of a binary representation of an integer can be done.

Summary: After subtracting an integer by 1 and then doing a bitwise AND operation with the original integer, the result is equivalent to turning the rightmost 1 in the binary representation of the integer into 0.

The application of bit arithmetic can be used in many situations:

    1. Clear 0 Special positioning (mask in the specific position 0, the other bit is 1, s = S & mask).
    2. Take a number in the middle to locate (a specific position in mask, the other bit is 0, s = S & mask).

Example : Enter two integers m and n to calculate how many bits in the binary representation of M need to be changed to get N.

WORKAROUND: The first step is to find the two-digit XOR; the second step is to count the 1 digits in the XOR result.

1 #include <iostream>2UsingNamespaceStd34IntMain ()5{6int a =Ten, B =, Count =0;int C; 8 C = a ^ B; 9 while (c) {10 c &= (C-111 count++;}13 cout<<count<<endl; 14 15 return 0;                 

Let's take another example to better illustrate the shift operation: Use a statement to determine if an integer is 2 of the entire number of times.

Workaround: An integer if it is 2 integer, then it has a binary representation and only one is 1, and all the other bits are 0. According to the previous analysis, subtract this integer by 1 and then do and operate with it, and the only 1 in this integer becomes 0.

Answer:! (x & (X-1))

Bit arithmetic problem

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.