Open In App

Unset least significant K bits of a given number

Given an integer N, the task is to print the number obtained by unsetting the least significant K bits from N.

Examples:



Input: N = 200, K=5
Output: 192
Explanation: 
(200)10 = (11001000)2 
Unsetting least significant K(= 5) bits from the above binary representation, the new number obtained is (11000000)2 = (192)10

Input: N = 730, K = 3
Output: 720



Approach: Follow the steps below to solve the problem:

 mask = ((~0) << K + 1) or 
mask = (-1 << K + 1) 

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the value
// after unsetting K LSBs
int clearLastBit(int N, int K)
{
    // Create a mask
    int mask = (-1 << K + 1);
 
    // Bitwise AND operation with
    // the number and the mask
    return N = N & mask;
}
 
// Driver Code
int main()
{
    // Given N and K
    int N = 730, K = 3;
 
    // Function Call
    cout << clearLastBit(N, K);
 
    return 0;
}




// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to return the value
// after unsetting K LSBs
static int clearLastBit(int N, int K)
{
    // Create a mask
    int mask = (-1 << K + 1);
 
    // Bitwise AND operation with
    // the number and the mask
    return N = N & mask;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given N and K
    int N = 730, K = 3;
 
    // Function Call
    System.out.print(clearLastBit(N, K));
}
}
 
// This code is contributed by shikhasingrajput




# Python3 program for the above approach
 
# Function to return the value
# after unsetting K LSBs
def clearLastBit(N, K):
 
    # Create a mask
    mask = (-1 << K + 1)
 
    # Bitwise AND operation with
    # the number and the mask
    N = N & mask
 
    return N
 
# Driver Code
 
# Given N and K
N = 730
K = 3
 
# Function call
print(clearLastBit(N, K))
 
# This code is contributed by Shivam Singh




// C# program for the above approach
using System;
class GFG{
 
// Function to return the value
// after unsetting K LSBs
static int clearLastBit(int N,
                        int K)
{
  // Create a mask
  int mask = (-1 << K + 1);
 
  // Bitwise AND operation with
  // the number and the mask
  return N = N & mask;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given N and K
  int N = 730, K = 3;
 
  // Function Call
  Console.Write(clearLastBit(N, K));
}
}
 
// This code is contributed by shikhasingrajput




<script>
 
// javascript program for the above approach
 
// Function to return the value
// after unsetting K LSBs
function clearLastBit(N , K)
{
    // Create a mask
    var mask = (-1 << K + 1);
 
    // Bitwise AND operation with
    // the number and the mask
    return N = N & mask;
}
 
// Driver Code
//Given N and K
var N = 730, K = 3;
 
// Function Call
document.write(clearLastBit(N, K));
 
// This code contributed by shikhasingrajput
 
</script>

Output: 
720

Time Complexity: O(1)
Auxiliary Space: O(1)


Article Tags :