Related Articles

Related Articles

Unset the last m bits
  • Difficulty Level : Expert
  • Last Updated : 22 May, 2019

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:



  1. Calculate num = (1 << (sizeof(int) * 8 – 1)) – 1. This will produce the highest positive integer num. All the bits in num will be set.
  2. Toggle the last m bits in num. Refer this post.
  3. Now, perform n = n & num. This will unset the last m bits in n.
  4. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to unset bits the last m bits
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php implementation to unset 
// bits the last m bits
  
// function to toggle 
// the last m bits
function 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
function 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 
?>

chevron_right



Output:

144

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :