Bit operations and practical skills (2): Advanced (1)

Source: Internet
Author: User

In binary, 1 has an odd or even number.
We can use the following code to calculate the parity of the number of 1 in a 32-bit integer binary. When the binary representation of input data contains an even number of 1, the program outputs 0, if there are odd numbers, output 1. For example, if there are 9 1 in binary 1314520 of 101000000111011011000, the program outputs 1 when x = 1314520.
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 the parity to judge the number of 1 in binary. The following code is strong. Can you see how this code works?
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. The result of the second XOR operation on this number 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.

Calculate the number of 1 in binary.
Assume that X is a 32-bit integer. After the following five assignments, the value of X is the number 1 in the binary representation of the original number. For example, if the initial value of X is 1314520 (crazy: Can you change the number?), then the final value of X is 9, which indicates that there are 9 1 in the binary 1314520.
x := (x and $55555555) + ((x shr 1) and $55555555);
x := (x and $33333333) + ((x shr 2) and $33333333);
x := (x and $0F0F0F0F) + ((x shr 4) and $0F0F0F0F);
x := (x and $00FF00FF) + ((x shr 8) and $00FF00FF);
x := (x and $0000FFFF) + ((x shr 16) and $0000FFFF);

For ease of explanation, the following describes how this program processes an 8-digit integer. Let's take the number 211 (the birthday of a certain 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 obtain the number of 1 in each two. For example, the first two digits 10 indicate that the first two digits of the original number have two ones. 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, we get the number of 1 in the entire binary. 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.

Returns the number of leading zeros in a 32-bit integer.
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.
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 questionActually, it was my first question in the noip simulation competition in the school. The question is as follows:

Returns a positive integer smaller than 2 ^ 32. This number 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.
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)
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 $0F0F0F0F) shl  4 or (x and $F0F0F0F0) shr  4;
   x := (x and $00FF00FF) shl  8 or (x and $FF00FF00) shr  8;
   x := (x and $0000FFFF) shl 16 or (x and $FFFF0000) shr 16;
   writeln(x);
end.

Its principle is roughly the same as the example of finding the number of 1 in binary. 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 strong
Writeln ('matrix ', 42 XOR 105, 'original, post, please specify the source ');

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.