📜  取消设置最后m位

📅  最后修改于: 2021-04-26 05:30:42             🧑  作者: Mango

给定一个非负数n 。问题是要取消设置n的二进制表示形式中的最后m位。

约束: 1 <= m <= num,其中numn的二进制表示形式中的位数。

例子:

Input : n = 10, m = 2
Output : 8
(10)10 = (1010)2
(8)10 = (1000)2
The last two bits in the binary 
representation of 10 have been unset.

Input : n = 150, m = 4
Output : 144

方法:以下是步骤:

  1. 计算num =(1 <<((sizeof(int)* 8 – 1))–1。这将产生最高的正整数numnum中的所有位都将被设置。
  2. 切换num中的最后m位。请参阅这篇文章。
  3. 现在,执行n = n&num 。这将取消设置n中的最后m位。
  4. 返回n

注意: sizeof(int)已用作int数据类型的输入。对于大型输入,可以使用long intlong long int数据类型代替int

C++
// C++ implementation to unset bits the last m bits
#include 
using namespace std;
  
// function to toggle the last m bits
unsigned int toggleLastMBits(unsigned int n,
                             unsigned int m)
{
    // calculating a number 'num' having 'm' bits
    // and all are set
    unsigned int num = (1 << m) - 1;
  
    // toggle the last m bits and return the number
    return (n ^ num);
}
  
// function to unset bits the last m bits
unsigned int unsetLastMBits(unsigned int n,
                            unsigned int m)
{
    // 'num' is the highest positive integer number
    // all the bits of 'num' are set
    unsigned int num = (1 << (sizeof(int) * 8 - 1)) - 1;
  
    // toggle the last 'm' bits in 'num'
    num = toggleLastMBits(num, m);
  
    // unset the last 'm' bits in 'n'
    // and return the number
    return (n & num);
}
  
// Driver program to test above
int main()
{
    unsigned int n = 150, m = 4;
    cout << unsetLastMBits(n, m);
    return 0;
}


Java
// Java implementation to unset
// bits the last m bits
class GFG 
{
      
static int sizeofInt = 8;
  
// function to toggle the last m bits
static int toggleLastMBits(int n,
                            int m)
{
    // calculating a number 'num' having 'm' bits
    // and all are set
    int num = (1 << m) - 1;
  
    // toggle the last m bits and return the number
    return (n ^ num);
}
  
// function to unset bits the last m bits
static int unsetLastMBits(int n,
                            int m)
{
    // 'num' is the highest positive integer number
    // all the bits of 'num' are set
    int num = (1 << (sizeofInt * 8 - 1)) - 1;
  
    // toggle the last 'm' bits in 'num'
    num = toggleLastMBits(num, m);
  
    // unset the last 'm' bits in 'n'
    // and return the number
    return (n & num);
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 150, m = 4;
    System.out.println(unsetLastMBits(n, m));
}
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation to unset
# bits the last m bits
import sys
  
# function to toggle the last m bits
def toggleLastMBits (n, m):
      
    # calculating a number 'num' 
    # having 'm' bits and all are set
    num = (1 << m) - 1
  
    # toggle the last m bits
    # and return the number
    return (n ^ num)
  
# function to unset bits
# the last m bits
def unsetLastMBits(n, m):
  
    # 'num' is the highest positive integer 
    # number all the bits of 'num' are set
    num = (1 << (sys.getsizeof(int) * 8 - 1)) - 1
  
    # toggle the last 'm' bits in 'num'
    num = toggleLastMBits(num, m)
  
    # unset the last 'm' bits in 'n'
    # and return the number
    return (n & num)
  
# Driven code
n = 150
m = 4
print (unsetLastMBits(n, m))
  
# This code is contributed by "rishabh_jain".


C#
// C# implementation to unset 
// bits the last m bits 
using System;
  
class GFG 
{ 
      
static int sizeofInt = 8; 
  
// function to toggle the last m bits 
static int toggleLastMBits(int n, 
                            int m) 
{ 
    // calculating a number 'num' having 'm' bits 
    // and all are set 
    int num = (1 << m) - 1; 
  
    // toggle the last m bits and return the number 
    return (n ^ num); 
} 
  
// function to unset bits the last m bits 
static int unsetLastMBits(int n, 
                            int m) 
{ 
    // 'num' is the highest positive integer number 
    // all the bits of 'num' are set 
    int num = (1 << (sizeofInt * 8 - 1)) - 1; 
  
    // toggle the last 'm' bits in 'num' 
    num = toggleLastMBits(num, m); 
  
    // unset the last 'm' bits in 'n' 
    // and return the number 
    return (n & num); 
} 
  
// Driver code 
public static void Main(String[] args) 
{ 
    int n = 150, m = 4; 
    Console.WriteLine(unsetLastMBits(n, m)); 
} 
} 
  
// This code contributed by Rajput-Ji


PHP


输出:

144