给定整数n和其中的两个比特位置p1和p2,在给定位置交换位。给定位置来自最低有效位(lsb)。例如,lsb的位置为0。
例子:
Input: n = 28, p1 = 0, p2 = 3
Output: 21
28 in binary is 11100. If we swap 0'th and 3rd digits,
we get 10101 which is 21 in decimal.
Input: n = 20, p1 = 2, p2 = 3
Output: 24
强烈建议您最小化浏览器,然后自己尝试。
方法1:
这个想法是首先找到这些位,然后使用基于XOR的交换概念,即交换两个数字’x’和’y’,我们做x = x ^ y,y = y ^ x和x = x ^ y。
下面是上述想法的实现
C++
// C++ program to swap bits in an integer
#include
using namespace std;
// This function swaps bit at positions p1 and p2 in an integer n
int swapBits(unsigned int n, unsigned int p1, unsigned int p2)
{
/* Move p1'th to rightmost side */
unsigned int bit1 = (n >> p1) & 1;
/* Move p2'th to rightmost side */
unsigned int bit2 = (n >> p2) & 1;
/* XOR the two bits */
unsigned int x = (bit1 ^ bit2);
/* Put the xor bit back to their original positions */
x = (x << p1) | (x << p2);
/* XOR 'x' with the original number so that the
two sets are swapped */
unsigned int result = n ^ x;
}
/* Driver program to test above function*/
int main()
{
int res = swapBits(28, 0, 3);
cout<<"Result = "<< res<<" ";
return 0;
}
// This code is contributed by pratham76.
C
// C program to swap bits in an integer
#include
// This function swaps bit at positions p1 and p2 in an integer n
int swapBits(unsigned int n, unsigned int p1, unsigned int p2)
{
/* Move p1'th to rightmost side */
unsigned int bit1 = (n >> p1) & 1;
/* Move p2'th to rightmost side */
unsigned int bit2 = (n >> p2) & 1;
/* XOR the two bits */
unsigned int x = (bit1 ^ bit2);
/* Put the xor bit back to their original positions */
x = (x << p1) | (x << p2);
/* XOR 'x' with the original number so that the
two sets are swapped */
unsigned int result = n ^ x;
}
/* Driver program to test above function*/
int main()
{
int res = swapBits(28, 0, 3);
printf("Result = %d ", res);
return 0;
}
Java
// Java program to swap bits in an integer
import java.io.*;
class GFG
{
// This function swaps bit at
// positions p1 and p2 in an integer n
static int swapBits( int n, int p1, int p2)
{
/* Move p1'th to rightmost side */
int bit1 = (n >> p1) & 1;
/* Move p2'th to rightmost side */
int bit2 = (n >> p2) & 1;
/* XOR the two bits */
int x = (bit1 ^ bit2);
/* Put the xor bit back to
their original positions */
x = (x << p1) | (x << p2);
/* XOR 'x' with the original
number so that the
two sets are swapped */
int result = n ^ x;
return result;
}
/* Driver code*/
public static void main (String[] args)
{
int res = swapBits(28, 0, 3);
System.out.println ("Result = " + res);
}
}
// This code is contributed by ajit..
C#
// C# program to swap bits in an integer
using System;
class GFG
{
// This function swaps bit at
// positions p1 and p2 in an integer n
static int swapBits( int n, int p1, int p2)
{
/* Move p1'th to rightmost side */
int bit1 = (n >> p1) & 1;
/* Move p2'th to rightmost side */
int bit2 = (n >> p2) & 1;
/* XOR the two bits */
int x = (bit1 ^ bit2);
/* Put the xor bit back to
their original positions */
x = (x << p1) | (x << p2);
/* XOR 'x' with the original
number so that the
two sets are swapped */
int result = n ^ x;
return result;
}
/* Driver code*/
public static void Main(string[] args)
{
int res = swapBits(28, 0, 3);
Console.Write("Result = " + res);
}
}
// This code is contributed by rutvik_56.
Javascript
C++
//C++ code for swapping given bits of a number
#include
using namespace std;
int swapBits(int n, int p1, int p2)
{
//left-shift 1 p1 and p2 times
//and using XOR
n ^= 1 << p1;
n ^= 1 << p2;
return n;
}
//Driver Code
int main()
{
cout << "Result = " << swapBits(28, 0, 3);
return 0;
}
//This code is contributed by yashbeersingh42
C
//C code for swapping given bits of a number
#include
int swapBits(int n, int p1, int p2)
{
//left-shift 1 p1 and p2 times
//and using XOR
n ^= 1 << p1;
n ^= 1 << p2;
return n;
}
//Driver Code
int main()
{
printf("Result = %d", swapBits(28, 0, 3));
return 0;
}
//This code is contributed by yashbeersingh42
Java
// Java code for swapping
// given bits of a number
import java.util.*;
class Main{
public static int swapBits(int n,
int p1,
int p2)
{
//left-shift 1 p1 and
// p2 times and using XOR
n ^= 1 << p1;
n ^= 1 << p2;
return n;
}
// Driver code
public static void main(String[] args)
{
System.out.print("Result = " +
swapBits(28, 0, 3));
}
}
// This code is contributed by divyeshrabadiya07
Python
# Python code for swapping given bits of a number
def swapBits(n, p1, p2):
# left-shift 1 p1 and p2 times
# and using XOR
n ^= 1 << p1
n ^= 1 << p2
return n
# Driver Code
print("Result =",swapBits(28, 0, 3))
# This code is contributed by rag2127
C#
// C# code for swapping given bits of a number
using System;
class GFG {
static int swapBits(int n, int p1, int p2)
{
// left-shift 1 p1 and p2 times
// and using XOR
n ^= 1 << p1;
n ^= 1 << p2;
return n;
}
// Driver code
static void Main() {
Console.WriteLine("Result = " + swapBits(28, 0, 3));
}
}
// This code is contributed by divyesh072019
Javascript
输出
Result = 21
C++
//C++ code for swapping given bits of a number
#include
using namespace std;
int swapBits(int n, int p1, int p2)
{
//left-shift 1 p1 and p2 times
//and using XOR
n ^= 1 << p1;
n ^= 1 << p2;
return n;
}
//Driver Code
int main()
{
cout << "Result = " << swapBits(28, 0, 3);
return 0;
}
//This code is contributed by yashbeersingh42
C
//C code for swapping given bits of a number
#include
int swapBits(int n, int p1, int p2)
{
//left-shift 1 p1 and p2 times
//and using XOR
n ^= 1 << p1;
n ^= 1 << p2;
return n;
}
//Driver Code
int main()
{
printf("Result = %d", swapBits(28, 0, 3));
return 0;
}
//This code is contributed by yashbeersingh42
Java
// Java code for swapping
// given bits of a number
import java.util.*;
class Main{
public static int swapBits(int n,
int p1,
int p2)
{
//left-shift 1 p1 and
// p2 times and using XOR
n ^= 1 << p1;
n ^= 1 << p2;
return n;
}
// Driver code
public static void main(String[] args)
{
System.out.print("Result = " +
swapBits(28, 0, 3));
}
}
// This code is contributed by divyeshrabadiya07
Python
# Python code for swapping given bits of a number
def swapBits(n, p1, p2):
# left-shift 1 p1 and p2 times
# and using XOR
n ^= 1 << p1
n ^= 1 << p2
return n
# Driver Code
print("Result =",swapBits(28, 0, 3))
# This code is contributed by rag2127
C#
// C# code for swapping given bits of a number
using System;
class GFG {
static int swapBits(int n, int p1, int p2)
{
// left-shift 1 p1 and p2 times
// and using XOR
n ^= 1 << p1;
n ^= 1 << p2;
return n;
}
// Driver code
static void Main() {
Console.WriteLine("Result = " + swapBits(28, 0, 3));
}
}
// This code is contributed by divyesh072019
Java脚本
输出
Result = 21