📜  切换数字中的第一个和最后一个比特以外的比特

📅  最后修改于: 2021-05-25 02:55:13             🧑  作者: Mango

给定一个数字,任务是切换除第一位和最后一位以外的数字位。
例子:

Input : 10
Output : 12
Binary representation:- 1 0 1 0
After toggling first and last : 1 1 0 0

Input : 9
Output : 15
Binary representation : 1 0 0 1
After toggling first and last : 1 1 1 1

先决条件:找到一个数字的最高有效位
1)生成一个数字,其中包含设置的中间位。我们需要将所有中间位更改为1,并将转角位保持为0。
2)答案是生成的数字和原始数字的XOR。请注意,将数字与1进行XOR即可切换数字。

C++
// C++ Program to toggle bits
// except first and last bit
#include
using namespace std;
 
// return set middle bits
int setmiddlebits(int n)
{
 
    // set all bit
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
 
    // return middle set bits
    // shift by 1 and xor with 1
    return (n >> 1) ^ 1;
}
 
int togglemiddlebits(int n)
{
     
    // if number is 1 then
    // simply return
    if (n == 1)
        return 1;
 
    // xor with
    // middle bits
    return n ^ setmiddlebits(n);
}
 
// Driver Code
int main()
{
     
    // Given number
    int n = 9;
 
    // print toggle bits
    cout<


Java
// Java program for toggle bits
// expect first and last bit
import java.io.*;
 
class GFG {
     
    // return set middle bits
    static int setmiddlebits(int n)
    {
 
        // set all bit
        n |= n >> 1;
        n |= n >> 2;
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
     
        // return middle set bits
        // shift by 1 and xor with 1
        return (n >> 1) ^ 1;
    }
 
    static int togglemiddlebits(int n)
    {
        // if number is 1 then
        // simply return
        if (n == 1)
            return 1;
     
        // XOR with middle bits
        return n ^ setmiddlebits(n);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
     
        // Given number
        int n = 9;
     
        // print toggle bits
        System.out.println(togglemiddlebits(n));
    }
}
 
// This code is contributed by vt_m


Python3
# Python3 program for toggle bits
# expect first and last bit
 
# return set middle bits
def setmiddlebits(n):
 
    # set all bit
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
 
    # return middle set bits
    # shift by 1 and xor with 1
    return (n >> 1) ^ 1
 
def togglemiddlebits(n):
 
    # if number is 1 then simply return
    if (n == 1):
        return 1
 
    # xor with middle bits
    return n ^ setmiddlebits(n)
 
# Driver code
n = 9
print(togglemiddlebits(n))
 
# This code is contributed by Anant Agarwal.


C#
// C# program for toggle bits
// expect first and last bit
using System;
 
class GFG {
     
    // return set middle bits
    static int setmiddlebits(int n)
    {
 
        // set all bit
        n |= n >> 1;
        n |= n >> 2;
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
         
        // return middle set bits
        // shift by 1 and xor with 1
        return (n >> 1) ^ 1;
    }
 
    static int togglemiddlebits(int n)
    {
         
        // if number is 1 then
        // simply return
        if (n == 1)
            return 1;
     
        // XOR with middle bits
        return n ^ setmiddlebits(n);
    }
     
    // Driver Code
    public static void Main ()
    {
     
        // Given number
        int n = 9;
     
        // print toggle bits
        Console.WriteLine(togglemiddlebits(n));
    }
}
 
// This code is contributed by Anant Agarwal.


PHP
> 1;
    $n |= $n >> 2;
    $n |= $n >> 4;
    $n |= $n >> 8;
    $n |= $n >> 16;
 
    // return middle set bits
    // shift by 1 and xor with 1
    return ($n >> 1) ^ 1;
}
 
function togglemiddlebits($n)
{
     
    // if number is 1 then
    // simply return
    if ($n == 1)
        return 1;
 
    // xor with
    // middle bits
    return $n ^ setmiddlebits($n);
}
 
    // Driver Code
    $n = 9;
 
    // print toggle bits
    echo togglemiddlebits($n);
 
// This code is contributed by ajit
?>


Javascript


时间复杂度:-O(1)
输出:

15