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)10Input: N = 730, K = 3
Output: 720
Approach: Follow the steps below to solve the problem:
- The idea is to create a mask of the form 111111100000….
- To create a mask, start from all ones as 1111111111….
- There are two possible options to generate all 1s. Either generate it by flipping all 0s with 1s or by using 2s complement and left shift it by K bits.
 mask = ((~0) << K + 1) orÂ
mask = (-1 << K + 1)Â
- Finally, print the value of K + 1 as it is zero-based indexing from the right to left.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the value// after unsetting K LSBsint 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 Codeint main(){Â Â Â Â // Given N and KÂ Â Â Â int N = 730, K = 3;Â
    // Function Call    cout << clearLastBit(N, K);Â
    return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{Â
// Function to return the value// after unsetting K LSBsstatic 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 Codepublic 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
# Python3 program for the above approach Â
# Function to return the value # after unsetting K LSBsdef 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 KN = 730K = 3Â
# Function callprint(clearLastBit(N, K))Â
# This code is contributed by Shivam Singh |
C#
// C# program for the above approachusing System;class GFG{Â
// Function to return the value// after unsetting K LSBsstatic 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 Codepublic 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 |
Javascript
<script>Â
// javascript program for the above approachÂ
// Function to return the value// after unsetting K LSBsfunction 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 Kvar N = 730, K = 3;Â
// Function Calldocument.write(clearLastBit(N, K));Â
// This code contributed by shikhasingrajput Â
</script> |
Output:Â
720
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



