Unset the last m bits

Given a non-negative number n. The problem is to unset the last m bits in the binary representation of n.
Constraint: 1 <= m <= num, where num is the number of bits in the binary representation of n.
Examples:
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
Approach: Following are the steps:
- Calculate num = (1 << (sizeof(int) * 8 – 1)) – 1. This will produce the highest positive integer num. All the bits in num will be set.
- Toggle the last m bits in num. Refer this post.
- Now, perform n = n & num. This will unset the last m bits in n.
- Return n.
Note: The sizeof(int) has been used as input is of int data type. For large inputs you can use long int or long long int datatypes in place of int.
C++
// C++ implementation to unset bits the last m bits#include <bits/stdc++.h>using namespace std;// function to toggle the last m bitsunsigned 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 bitsunsigned 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 aboveint main(){ unsigned int n = 150, m = 4; cout << unsetLastMBits(n, m); return 0;} |
Java
// Java implementation to unset// bits the last m bitsclass GFG { static int sizeofInt = 8;// function to toggle the last m bitsstatic 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 bitsstatic 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 codepublic static void main(String[] args) { int n = 150, m = 4; System.out.println(unsetLastMBits(n, m));}}/* This code is contributed by PrinciRaj1992 */ |
Python3
# Python3 implementation to unset# bits the last m bitsimport sys# function to toggle the last m bitsdef 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 bitsdef 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 coden = 150m = 4print (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 is contributed by Rajput-Ji |
PHP
<?php// php implementation to unset // bits the last m bits// function to toggle // the last m bitsfunction 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 bitsfunction unsetLastMBits($n,$m){ // 'num' is the highest positive // integer number all the bits // of 'num' are set //4 is Size of integer 32 bit $num = (1 << (4 * 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);} // Drivercode $n = 150; $m = 4; echo unsetLastMBits($n, $m);// This code is contributed by mits ?> |
Javascript
<script>// JavaScript implementation to unset// bits the last m bitslet sizeofLet = 8; // Function to toggle the last m bitsfunction toggleLastMBits(n, m){ // Calculating a number 'num' having // 'm' bits and all are set let num = (1 << m) - 1; // Toggle the last m bits and // return the number return (n ^ num);} // Function to unset bits the last m bitsfunction unsetLastMBits(n, m){ // 'num' is the highest positive // integer number all the bits of // 'num' are set let num = (1 << (sizeofLet * 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 Codelet n = 150, m = 4;document.write(unsetLastMBits(n, m));// This code is contributed by sanjoy_62</script> |
Output:
144
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!



