📜  从给定数字的 LSB 开始翻转连续的设置位

📅  最后修改于: 2021-10-27 03:19:15             🧑  作者: Mango

给定一个正整数N ,任务是找到通过翻转从N的二进制表示中的 LSB 开始的连续设置位可以获得的数字。

例子:

朴素的方法:最简单的方法是通过执行N1 的逻辑 AND( & )直到N & 1不为0并保持计数来找到从 LSB 开始的连续设置位的数量 设置位数。然后,简单地将N左移设置位的计数。打印获得的数字作为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number after
// converting 1s from end to 0s
int findNumber(int N)
{
    // Count of 1s
    int count = 0;
 
    // AND operation of N and 1
    while ((N & 1) == 1) {
        N = N >> 1;
        count++;
    }
 
    // Left shift N by count
    return N << count;
}
 
// Driver Code
int main()
{
    int N = 39;
 
    // Function Call
    cout << findNumber(N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
     
    // Count of 1s
    int count = 0;
 
    // AND operation of N and 1
    while ((N & 1) == 1)
    {
        N = N >> 1;
        count++;
    }
 
    // Left shift N by count
    return N << count;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 39;
     
    // Function Call
    System.out.println(findNumber(N));
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to find the number after
# converting 1s from end to 0s
def findNumber(N):
     
    # Count of 1s
    count = 0
 
    # AND operation of N and 1
    while ((N & 1) == 1):
        N = N >> 1
        count += 1
 
    # Left shift N by count
    return N << count
 
# Driver Code
if __name__ == "__main__":
 
    N = 39
 
    # Function Call
    print(findNumber(N))
 
# This code is contributed by AnkThon


C#
// C# program to implement
// the above approach 
using System;
  
class GFG{
      
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
      
    // Count of 1s
    int count = 0;
  
    // AND operation of N and 1
    while ((N & 1) == 1)
    {
        N = N >> 1;
        count++;
    }
  
    // Left shift N by count
    return N << count;
}
 
// Driver Code
public static void Main()
{
    int N = 39;
      
    // Function Call
    Console.WriteLine(findNumber(N));
}
}
 
// This code is contributed by code_hunt


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number after
// converting 1s from end to 0s
int findNumber(int N)
{
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
int main()
{
    int N = 39;
 
    // Function Call
    cout << findNumber(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
   
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 39;
 
    // Function Call
    System.out.print(findNumber(N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find the number after
# converting 1s from end to 0s
def findNumber(N):
     
    # Return the logical AND
    # of N and (N + 1)
    return N & (N + 1)
 
# Driver Code
if __name__ == '__main__':
    N = 39
 
    # Function Call
    print(findNumber(N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG
{
 
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
   
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 39;
 
    // Function Call
    Console.Write(findNumber(N));
}
}
  
// This code is contributed by 29AjayKumar


Javascript


输出:
32

时间复杂度: O(log N)
辅助空间: O(1)

高效方法:要优化上述方法,请找到N(N + 1)逻辑与 ( & ) 。关键的观察结果是,将数字加1会使 LSB 中的每个连续设置位变为0 。因此, N & (N + 1)给出了所需的数字。

插图:

N = 39, therefore (N+1)=40
⇒   N = 39 = (100111)
⇒ N+1 = 40 = (101000)
Performing Logical AND(&) operation:
  1 0 0 1 1 1 
& 1 0 1 0 0 0
----------------
  1 0 0 0 0 0  ⇒ 32
----------------

Will this always work? Add 1 to N:
 1 0 0 1 1 1
 +         1
 ------------- 
 1 0 1 0 0 0    
 --------------     
It can be clearly seen that the continous set bits from the LSB becomnes unset.

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number after
// converting 1s from end to 0s
int findNumber(int N)
{
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
int main()
{
    int N = 39;
 
    // Function Call
    cout << findNumber(N);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
   
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 39;
 
    // Function Call
    System.out.print(findNumber(N));
}
}
 
// This code is contributed by 29AjayKumar

蟒蛇3

# Python3 program for the above approach
 
# Function to find the number after
# converting 1s from end to 0s
def findNumber(N):
     
    # Return the logical AND
    # of N and (N + 1)
    return N & (N + 1)
 
# Driver Code
if __name__ == '__main__':
    N = 39
 
    # Function Call
    print(findNumber(N))
 
# This code is contributed by mohit kumar 29

C#

// C# program for the above approach
using System;
 
class GFG
{
 
// Function to find the number after
// converting 1s from end to 0s
static int findNumber(int N)
{
   
    // Return the logical AND
    // of N and (N + 1)
    return N & (N + 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 39;
 
    // Function Call
    Console.Write(findNumber(N));
}
}
  
// This code is contributed by 29AjayKumar

Javascript


输出:
32

时间复杂度: O(log N)
辅助空间: O(1)