📜  设置位仅在第L个和第R个索引之间的数字

📅  最后修改于: 2021-04-27 17:40:43             🧑  作者: Mango

给定L和R。任务是找到一个数字,该数字以二进制表示形式设置第L个和第R个索引之间的所有位,而其余位未设置。二进制表示形式为32位。

例子:

天真的方法:天真的方法是从i = L迭代到i = R并计算2 i的所有幂的加法。

下面的程序说明了幼稚的方法:

C++
// CPP program to print the integer
// with all the bits set in range L-R
// Naive Approach
#include 
using namespace std;
  
// Function to return the integer
// with all the bits set in range L-R
int getInteger(int L, int R)
{
  
    int number = 0;
  
    // iterate from L to R
    // and add all powers of 2
    for (int i = L; i <= R; i++)
        number += pow(2, i);
  
    return number;
}
  
// Driver Code
int main()
{
    int L = 2, R = 5;
    cout << getInteger(L, R);
    return 0;
}


Java
// Java program to print the 
// integer with all the bits 
// set in range L-R Naive Approach
import java.io.*;
  
class GFG 
{
  
// Function to return the 
// integer with all the 
// bits set in range L-R
static int getInteger(int L, 
                      int R)
{
    int number = 0;
  
    // iterate from L to R
    // and add all powers of 2
    for (int i = L; i <= R; i++)
        number += Math.pow(2, i);
  
    return number;
}
  
// Driver Code
public static void main (String[] args) 
{
    int L = 2, R = 5;
    System.out.println(getInteger(L, R));
}
}
  
// This code is contributed by anuj_67..


Python3
# Python 3 program to print the integer
# with all the bits set in range L-R
# Naive Approach
from math import pow
  
# Function to return the integer
# with all the bits set in range L-R
def getInteger(L, R):
    number = 0
  
    # iterate from L to R
    # and add all powers of 2
    for i in range(L, R + 1, 1):
        number += pow(2, i)
  
    return number
  
# Driver Code
if __name__ == '__main__':
    L = 2
    R = 5
    print(int(getInteger(L, R)))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to print the 
// integer with all the bits 
// set in range L-R Naive Approach
using System;
  
class GFG
{
// Function to return the 
// integer with all the 
// bits set in range L-R
static int getInteger(int L, 
                      int R)
{
    int number = 0;
  
    // iterate from L to R
    // and add all powers of 2
    for (int i = L; i <= R; i++)
        number += (int)Math.Pow(2, i);
  
    return number;
}
  
// Driver Code
public static void Main () 
{
    int L = 2, R = 5;
    Console.Write(getInteger(L, R));
}
}
  
// This code is contributed
// by shiv_bhakt.


PHP


C++
// CPP program to print the integer
// with all the bits set in range L-R
// Efficient Approach
#include 
using namespace std;
  
// Function to return the integer
// with all the bits set in range L-R
int setbitsfromLtoR(int L, int R)
{
    return (1 << (R + 1)) - (1 << L);
}
  
// Driver Code
int main()
{
    int L = 2, R = 5;
    cout << setbitsfromLtoR(L, R);
    return 0;
}


Java
// Java program to print 
// the integer with all 
// the bits set in range
// L-R Efficient Approach
import java.io.*;
  
class GFG 
{
      
// Function to return the 
// integer with all the 
// bits set in range L-R
static int setbitsfromLtoR(int L, 
                           int R)
{
    return (1 << (R + 1)) - 
           (1 << L);
}
  
// Driver Code
public static void main (String[] args)
{
    int L = 2, R = 5;
    System.out.println(setbitsfromLtoR(L, R));
}
}
  
// This code is contributed
// by shiv_bhakt.


Python3
# Python3 program to print 
# the integer with all the 
# bits set in range L-R
# Efficient Approach
  
# Function to return the
# integer with all the
# bits set in range L-R
def setbitsfromLtoR(L, R):
  
    return ((1 << (R + 1)) - 
            (1 << L))
  
# Driver Code
L = 2
R = 5
print(setbitsfromLtoR(L, R))
  
# This code is contributed
# by Smita


C#
// C# program to print 
// the integer with all 
// the bits set in range
// L-R Efficient Approach
using System;
  
class GFG
{
// Function to return the 
// integer with all the 
// bits set in range L-R
static int setbitsfromLtoR(int L, 
                           int R)
{
    return (1 << (R + 1)) - 
           (1 << L);
}
  
// Driver Code
public static void Main ()
{
    int L = 2, R = 5;
    Console.WriteLine(setbitsfromLtoR(L, R));
}
}
  
// This code is contributed
// by shiv_bhakt.


PHP


输出:

60

一种有效的方法是从右开始计算所有(R)个设置位的数量,然后从右开始计算所有(L-1)个位设置的数量,以获得所需的数量。

  1. 使用以下公式从右计算具有所有R设置位的数字。
    (1 << (R+1)) - 1.
  2. 从右边减去所有(L-1)个设置位的数字。
    (1<

因此计算((1 <<(R + 1))-1)-((1 << L)-1),我们得到的最终公式为:

下面的程序说明了有效的方法:

C++

// CPP program to print the integer
// with all the bits set in range L-R
// Efficient Approach
#include 
using namespace std;
  
// Function to return the integer
// with all the bits set in range L-R
int setbitsfromLtoR(int L, int R)
{
    return (1 << (R + 1)) - (1 << L);
}
  
// Driver Code
int main()
{
    int L = 2, R = 5;
    cout << setbitsfromLtoR(L, R);
    return 0;
}

Java

// Java program to print 
// the integer with all 
// the bits set in range
// L-R Efficient Approach
import java.io.*;
  
class GFG 
{
      
// Function to return the 
// integer with all the 
// bits set in range L-R
static int setbitsfromLtoR(int L, 
                           int R)
{
    return (1 << (R + 1)) - 
           (1 << L);
}
  
// Driver Code
public static void main (String[] args)
{
    int L = 2, R = 5;
    System.out.println(setbitsfromLtoR(L, R));
}
}
  
// This code is contributed
// by shiv_bhakt.

Python3

# Python3 program to print 
# the integer with all the 
# bits set in range L-R
# Efficient Approach
  
# Function to return the
# integer with all the
# bits set in range L-R
def setbitsfromLtoR(L, R):
  
    return ((1 << (R + 1)) - 
            (1 << L))
  
# Driver Code
L = 2
R = 5
print(setbitsfromLtoR(L, R))
  
# This code is contributed
# by Smita

C#

// C# program to print 
// the integer with all 
// the bits set in range
// L-R Efficient Approach
using System;
  
class GFG
{
// Function to return the 
// integer with all the 
// bits set in range L-R
static int setbitsfromLtoR(int L, 
                           int R)
{
    return (1 << (R + 1)) - 
           (1 << L);
}
  
// Driver Code
public static void Main ()
{
    int L = 2, R = 5;
    Console.WriteLine(setbitsfromLtoR(L, R));
}
}
  
// This code is contributed
// by shiv_bhakt.

的PHP


输出:

60

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