Bit operation introduction and practical skills

Source: Internet
Author: User

Subscription | previous | next

Program impossible Bit operations and practical skills (2): Advanced (1)Author: matrix67 Date: font size: Small Medium large ===== something really strong! =====

In binary, 1 has an odd or even number.
We can use the followingCodeComeComputingIn the binary of a 32-bit integerNumberWhen the binary representation of input data has an even numberNumberWord 1 hourProgramOutput 0. If there are odd numbers, output 1. For exampleBinary 1If there are 9 1 in 01000000111011011000 and x = 1314520, the program outputs 1. Program code

VaR
I, X, C: longint;
Begin
Readln (X );
C: = 0;
For I: = 1 to 32 do
Begin
C: = C + X and 1;
X: = x SHR 1;
End;
Writeln (C and 1 );
End.

However, this is not very efficient, and the magic of bit operations has not yet been reflected.
It is also used to determine the value of 1 in binary.NumberParity, the following code is strong. Can you see how this code works? Program code

 
VaR
X: longint;
Begin
Readln (X );
X: = x XOR (x SHR 1 );
X: = x XOR (x SHR 2 );
X: = x XOR (x SHR 4 );
X: = x XOR (x SHR 8 );
X: = x XOR (x SHR 16 );
Writeln (X and 1 );
End.

To illustrate the principles of the above Code, let's talk about it with 1314520. The binary value of 1314520 is 101000000111011011000. The result of the first XOR operation is as follows:

00000000000101000000111011011000
XOR 0000000000010100000011101101100
---------------------------------------
00000000000111100000100110110100

The result is a new binary number. the number on the I-bit from the right indicates whether there are odd numbers 1 or even numbers 1 on the I and I + 1 of the original number. For example, the 0 on the rightmost indicates that the last two digits of the original number have an even number of 1, and the 1 on the first 3rd bits on the right indicates that the position of the original number has an odd number of 1 and the previous position. For thisNumberThe result of the second XOR operation is as follows:

00000000000111100000100110110100
XOR 000000000001111000001001101101
---------------------------------------
00000000000110011000101111011001

In the result, each 1 indicates that there is an odd number of 1 in the position of the original number and the first three positions. Each 0 indicates an even number of 1 in the four positions corresponding to the original number. After the fifth XOR or termination, the last bit of the binary number indicates the number of 1 digits in the 32-digit system. This is the final answer we want.

Computing1 in binaryNumber
Assume that X is a 32-bit integer. After the following five values are assigned, the value of X is the binary representation of the original number.Number1'sNumber. For example, the initial value of X is 1314520 (crazy: Can I change it ?)NumberAH), then the final X is changed to 9, which indicates that there are 9 1 in the binary 1314520. Program code

 
X: = (X and $55555555) + (x SHR 1) and $55555555 );
X: = (X and $33333333) + (x SHR 2) and $33333333 );
X: = (X and $ 0f0f0f) + (x SHR 4) and $ 0f0f0f );
X: = (X and $ 00ff00ff) + (x SHR 8) and $ 00ff00ff );
X: = (X and $0000 FFFF) + (x SHR 16) and $0000 FFFF );

For ease of explanation, the following describes how this program processes an 8-digit integer. TakeNumber211 (the birthday of a mm in our class. The binary 211 is 11010011.

+ --- +
| 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | <--- original number
+ --- +
| 1 0 | 0 1 | 0 0 | 1 0 | <--- after the first operation
+ ------- +
| 0 0 1 1 | 0 0 1 0 | <--- after the second operation
+ --------------- +
| 0 0 0 0 0 1 0 1 1 | <--- after the third operation, the number is 5
+ ------------------------------- +

The whole program is a sub-governance idea. For the first time, we add each adjacent two to get one of every twoNumberFor example, the first two digits 10 indicate that the first two digits of the original number have two 1. For the second time, we continue to add 10 + 01 = 11,00 + 10 = 10. The result is 00110010, which indicates that the first four digits of the original number have 3 1, the last four digits have two ones. The last time we add 0011 and 0010 together, we get 1 of the entire binary.Number. The program cleverly uses the bitwise AND right shift. For example, the binary value of $33333333 in the second row is 00110011001100 ...., using it and X for the and operation is equivalent to taking 2 as the unit interval. The role of SHR is to align the same number of addition operations.

Binary Search for leading 0 of a 32-bit integerNumber
The C language is used here. I directly copy the code on Hacker's delight. It looks better to write this code into C. If you write it into Pascal, there will be a lot of begin and end, which makes the code very ugly. The program thought is binary search. It should be very simple. I will not elaborate on it. Program code

Int nlz (unsigned X)
{
Int N;

If (x = 0) Return (32 );
N = 1;
If (x> 16) = 0) {n = N + 16; X = x <16 ;}
If (x> 24) = 0) {n = N + 8; X = x <8 ;}
If (x> 28) = 0) {n = N + 4; X = x <4 ;}
If (x> 30) = 0) {n = n + 2; X = x <2 ;}
N = N-(x> 31 );
Return N;
}

Use bitwise operations to obtain the absolute value
This is a very interesting question. Let's think about it first. CTRL + A shows the answer.
Answer: If X is a 32-bit integer, the result of x xor (not (x SHR 31) + 1) + x SHR 31 is the absolute value of X.
X shr 31 is the highest bit of binary, which is used to represent the symbol of X. If it is 0 (X is positive), not (x SHR 31) + 1 is equal to $00000000, the results of the difference or any number remain unchanged. If the highest bit is 1 (X is negative ), then not (x SHR 31) + 1 is equal to $ ffffff, X is different or it is equivalent to all digits to reverse, and then add one after the difference or.

High/low bit Switching
This is actually my first question in the noip simulation competition in the school. The reference content is a positive integer smaller than 2 ^ 32. ThisNumberIt can be expressed by a 32-bit binary number (less than 32 bits are supplemented by 0 ). We call the first 16 bits of this binary number as "high", and the last 16 bits as "low ". We can get a new number by exchanging its high and low bits. Ask the new number in decimal format ).
For example, the number 1314520 is expressed as 0000 0000 0001 0100 0000 1110 1101 1000 in binary format (11 leading 0 s are added to complement 32 bits), where the first 16 bits are high, that is, 0000 0000 0001 0100; the last 16 digits are low, that is, 0000 1110 1101 1000. By switching its high and low bits, we get a new binary number of 0000 1110 1101 1000 0000 0000 0001 0100. It is the decimal 249036820.

At that time, almost no one thought of replacing the lengthy program with a single-digit operation. The bitwise operation is complete. Program code

 
VaR
N: DWORD;
Begin
Readln (N );
Writeln (n SHR 16) or (n SHL 16 ));
End.

In fact, Pascal has a system function swap that can be used directly.

Binary Reverse Order
The following program reads a 32-bit integer and outputs the number indicated by its binary descending order.
Input: 1314520 (Binary 00000000000101000000111011011000)
Output: 460335104 (Binary 00011011011100000010100000000000)Program code

 
VaR
X: DWORD;
Begin
Readln (X );
X: = (X and $55555555) SHL 1 or (X and $ aaaaaaaa) SHR 1;
X: = (X and $33333333) SHL 2 or (X and $ cccccccc) SHR 2;
X: = (X and $ 0f0f0f) SHL 4 or (X and $ f0f0f0f0) SHR 4;
X: = (X and $ 00ff00ff) SHL 8 or (X and $ ff00ff00) SHR 8;
X: = (X and $0000 FFFF) SHL 16 or (X and $ ffff0000) SHR 16;
Writeln (X );
End.

Its principle is equivalent to the one in the binary.NumberThe example is roughly the same. The program first exchanges the number on each adjacent two, and later regards the number exchanged with each other as a whole, and continues the switching operation in the unit of 2 bits and 4 bits. We use an 8-digit integer 211 to demonstrate the program execution process again:
+ --- +
| 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | <--- original number
+ --- +
| 1 1 | 1 0 | 0 0 | 1 1 | <--- after the first operation
+ ------- +
| 1 0 1 1 | 1 1 0 0 | <--- after the second operation
+ --------------- +
| 1 1 0 0 1 0 1 1 1 | <--- after the third operation
+ ------------------------------- +

Copyright is also very strongProgram code

  writeln ('matrix ', 42 XOR 105, 'original, please indicate the authorization');  

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.