# P1201 high and low level Exchange problem solving report

Source: Internet
Author: User
name:p1201 high and low level switching
Copyright: A column originating from goal00001111, which allows for free reproduction, but must indicate the author and source
author:goal00001111
date:17-12-08 09:24
Description:
Description Description
A positive integer less than 2^32 is given. This number can be represented by a 32-bit binary number (less than 32 bits with 0 complement).
We call the first 16 bits of the binary number "high" and the last 16 bits "low." We can get a new number by exchanging its high and low positions.
How much this new number is (in decimal notation).
For example, the number 1314520 is represented by a binary representation of 0000 0000 0001 0100 0000 1110 1101 1000 (11 leading 0 are added to 32 bits),
The first 16 bits are high, i.e. 0000 0000 0001 0100; the latter 16 bits are low, i.e. 0000 1110 1101 1000. To exchange its high and low positions,
We got a new binary number 0000 1110 1101 1000 0000 0000 0001 0100. It is the decimal 249036820.
Input format
A positive integer less than 2^32
Output format
The new number is output
Sample input to sample
1314520
Sample output Sample Outputs
249036820

Topic Analysis:
This is a question of examining bit operations, and if we are familiar with bit arithmetic, the main code can be as simple as one line.
I will analyze the usual methods and skillfully utilize the bit operations in turn.
Method One: The conventional method is to simulate the conversion process of decimal and binary directly according to test instructions. Although the method is cumbersome,
But it can help us to deepen the understanding of the conversion, especially decimal conversion to binary, the use of bit operations and operations, get your values, very ingenious.
Method Two: Using a certain mathematical analysis, notice the low 16 digits of n = n (2^16); Higher 16-digit high = N-low;
The low* (2^16) is then turned into a high 16-bit, and the higher/(2^16) becomes a low 16-bit, which completes the conversion.
Method Three: The ingenious application of bit arithmetic, shift n about 16 bits, then add on the line, then execute (n << 16) | (n >> 16).

Description
Algorithmic thinking: direct simulation or mathematical analysis.
Data structures: arrays.
Time complexity: O (1);;
Space complexity: Method One is O (32), method Two, three does not need auxiliary space;
Programming languages: Implemented in C + + and Pascal, respectively.
Note: The three methods, such as direct simulation and mathematical analysis, are provided respectively.
For more information on bit operations, please refer to: Matrix67 's blog http://www.matrix67.com/blog/archives/263

C + + code:

#include <iostream>

using namespace Std;

unsigned long long simulate (unsigned long long n);//Direct simulation method
unsigned long long analyse (unsigned long long n); Mathematical Analysis Method 1: Indirect displacement
unsigned long long Shift (unsigned long long n); Mathematical Analysis Method 2: Direct displacement

int main ()
{
unsigned long long n;
CIN >> N;

cout << Analyse (n) << Endl;
cout << Simulate (n) << Endl;
cout << Shift (n) << Endl;

System ("pause");
return 0;
}

Direct Simulation Method: First convert decimal number to binary, high and low change the position and then change back
unsigned long long simulate (unsigned long long N)
{
unsigned long long a[32] = {1};//storage 2^n (n=0,1,2,..., 31)
int b[32] = {0}; Used to store binary numbers

for (int i=1; i<32; i++)//Compute and store 2^n (n=0,1,2,..., 31)
{
A[i] = a[i-1] * 2;
}

for (int i=0; i<32; i++)//Convert binary number
{
B[31-i] = (A[i] & N)! = 0;
}

for (int i=0; i<16; i++)//high/low transposition
{
int temp = B[i];
B[i] = b[i+16];
B[I+16] = temp;
}

n = 0;
for (int i=0; i<32; i++)//Convert to decimal number
{
n + = a[i] * B[31-i];
}
return n;
}

Mathematical analysis method: To take the high and low 16-bit, the shift operation transposition
unsigned long long analyse (unsigned long long N)
{
Const unsigned long long MAX = 65536; MAX = 2^16
unsigned long long dest, high, low;

Low = n% MAX; Take 16 digits lower
High = n-low;//takes 16 digits
Dest = low * max;//shifts the lower 16 digits to the left 16 bits, which is multiplied by 2^16
Dest + = high/max;//The high 16 digits to the right 16 bits, which is divided by 2^16

return dest;
}

Mathematical Analysis Method: Transposition of shift operation directly
unsigned long long Shift (unsigned long long N)
{
Return (n << 16) | (n >> 16);
}

Pascal Code:

Program P1201 (INPUT, OUTPUT);
Var
N:longword;

{Mathematical analysis method: To take the high and low 16-bit, the shift operation transposition}
FUNCTION Analyse (N:longword): Longword;
Const
MAX = 65536; {MAX = 2^16}
Var
Dest, High, Low:longword;
Begin
Low: = n MoD MAX; {Take down 16-digit number}
High: = N-low; {Take high 16-digit number}
Dest: = Low * MAX; {Move the low 16-digit number to the left 16-bit, which is multiplied by 2^16}
Dest: = dest + high div MAX; {Move the high 16-digit number right 16 bits, dividing by 2^16}

Analyse: = dest;
End {Analys}

{Mathematical analysis method: Direct shift Operation Transposition}
FUNCTION Shift (N:longword): Longword;
Begin
Shift: = (n shr) or (n SHL 16);
End {Shift}

{Direct Simulation method: First convert decimal number to binary, high and low transposition and return}
FUNCTION Simulate (N:longword): Longword;
Var
A:array [1..32] of Longword; {Storage 2^n (n=0,1,2,..., 31)}
B:array [1..32] of Integer; {Used to store binary number}
I, Temp:integer;
Begin
A[1]: = 1;
For i:=2 to + do {storage 2^n (n=0,1,2,..., 31)}
A[i]: = a[i-1] * 2;

For I:=1 to + do {convert binary number}
B[33-i]: = Ord ((A[i] and N) <> 0);

For I:=1 to + do {high and low transposition}
Begin
Temp: = B[i];
B[i]: = b[i+16];
B[I+16]: = temp;
End

N: = 0;
For I:=1 to + do {convert to decimal number}
N: = n + a[i] * B[33-i];

Simulate: = N;
End {Simulate}

BEGIN {main}
Writeln (Analyse (n));
Writeln (Simulate (n));
Writeln (Shift (n));
END.
Related Keywords:

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.