Unset the last m bits
Last Updated :
15 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
unsigned int toggleLastMBits(unsigned int n,
unsigned int m)
{
unsigned int num = (1 << m) - 1;
return (n ^ num);
}
unsigned int unsetLastMBits(unsigned int n,
unsigned int m)
{
unsigned int num = (1 << ( sizeof ( int ) * 8 - 1)) - 1;
num = toggleLastMBits(num, m);
return (n & num);
}
int main()
{
unsigned int n = 150, m = 4;
cout << unsetLastMBits(n, m);
return 0;
}
|
Java
class GFG
{
static int sizeofInt = 8 ;
static int toggleLastMBits( int n,
int m)
{
int num = ( 1 << m) - 1 ;
return (n ^ num);
}
static int unsetLastMBits( int n,
int m)
{
int num = ( 1 << (sizeofInt * 8 - 1 )) - 1 ;
num = toggleLastMBits(num, m);
return (n & num);
}
public static void main(String[] args)
{
int n = 150 , m = 4 ;
System.out.println(unsetLastMBits(n, m));
}
}
|
Python3
import sys
def toggleLastMBits (n, m):
num = ( 1 << m) - 1
return (n ^ num)
def unsetLastMBits(n, m):
num = ( 1 << (sys.getsizeof( int ) * 8 - 1 )) - 1
num = toggleLastMBits(num, m)
return (n & num)
n = 150
m = 4
print (unsetLastMBits(n, m))
|
C#
using System;
class GFG
{
static int sizeofInt = 8;
static int toggleLastMBits( int n,
int m)
{
int num = (1 << m) - 1;
return (n ^ num);
}
static int unsetLastMBits( int n,
int m)
{
int num = (1 << (sizeofInt * 8 - 1)) - 1;
num = toggleLastMBits(num, m);
return (n & num);
}
public static void Main(String[] args)
{
int n = 150, m = 4;
Console.WriteLine(unsetLastMBits(n, m));
}
}
|
PHP
<?php
function toggleLastMBits( $n , $m )
{
$num = (1 << $m ) - 1;
return ( $n ^ $num );
}
function unsetLastMBits( $n , $m )
{
$num = (1 << (4 * 8 - 1)) - 1;
$num = toggleLastMBits( $num , $m );
return ( $n & $num );
}
$n = 150;
$m = 4;
echo unsetLastMBits( $n , $m );
?>
|
Javascript
<script>
let sizeofLet = 8;
function toggleLastMBits(n, m)
{
let num = (1 << m) - 1;
return (n ^ num);
}
function unsetLastMBits(n, m)
{
let num = (1 << (sizeofLet * 8 - 1)) - 1;
num = toggleLastMBits(num, m);
return (n & num);
}
let n = 150, m = 4;
document.write(unsetLastMBits(n, m));
</script>
|
Output:
144
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...