Given a number, the task is to toggle bits of the number except the first and the last bit.
Examples:
Input : 10
Output : 12
Binary representation:- 1 0 1 0
After toggling first and last : 1 1 0 0
Input : 9
Output : 15
Binary representation : 1 0 0 1
After toggling first and last : 1 1 1 1
Prerequisite: Find most significant set bit of a number
- Generate a number that contains the middle bit as a set. We need to change all middle bits to 1 and keep corner bits as
- The answer is XOR of generated number and original number. Note that XOR of 1 with a number toggles the number.
C++
#include<iostream>
using namespace std;
int setmiddlebits( int n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return (n >> 1) ^ 1;
}
int togglemiddlebits( int n)
{
if (n == 1)
return 1;
return n ^ setmiddlebits(n);
}
int main()
{
int n = 9;
cout<<togglemiddlebits(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int setmiddlebits( int n)
{
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
return (n >> 1 ) ^ 1 ;
}
static int togglemiddlebits( int n)
{
if (n == 1 )
return 1 ;
return n ^ setmiddlebits(n);
}
public static void main (String[] args)
{
int n = 9 ;
System.out.println(togglemiddlebits(n));
}
}
|
Python3
def setmiddlebits(n):
n | = n >> 1 ;
n | = n >> 2 ;
n | = n >> 4 ;
n | = n >> 8 ;
n | = n >> 16 ;
return (n >> 1 ) ^ 1
def togglemiddlebits(n):
if (n = = 1 ):
return 1
return n ^ setmiddlebits(n)
n = 9
print (togglemiddlebits(n))
|
C#
using System;
class GFG {
static int setmiddlebits( int n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return (n >> 1) ^ 1;
}
static int togglemiddlebits( int n)
{
if (n == 1)
return 1;
return n ^ setmiddlebits(n);
}
public static void Main ()
{
int n = 9;
Console.WriteLine(togglemiddlebits(n));
}
}
|
PHP
<?php
function setmiddlebits( $n )
{
$n |= $n >> 1;
$n |= $n >> 2;
$n |= $n >> 4;
$n |= $n >> 8;
$n |= $n >> 16;
return ( $n >> 1) ^ 1;
}
function togglemiddlebits( $n )
{
if ( $n == 1)
return 1;
return $n ^ setmiddlebits( $n );
}
$n = 9;
echo togglemiddlebits( $n );
?>
|
Javascript
<script>
function setmiddlebits(n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return (n >> 1) ^ 1;
}
function togglemiddlebits(n)
{
if (n == 1)
return 1;
return n ^ setmiddlebits(n);
}
var n = 9;
document.write(togglemiddlebits(n));
</script>
|
Time Complexity: O(log2n), where n is the given number
Auxiliary Space: O(1)
Another Approach: Using bit mask
The idea to solve this problem is that we can use XOR of a specific bit with 1 to toggle the specific bit. Therefore, for an n – bit number, we can construct a bit mask of the form 0111….11110, ie, an n – bit number, where all bits are set except the first and last bit. Therefore, for a number that is greater than or equal to 4, the bit mask is equal to 2n – 2. (n can be calculated using log2number) (For numbers less than 4, there are no middle bits, therefore, the number will not be changed).
Approach:
- Calculate the bit mask = 2 log2number – 2
- Perform the XOR of the number and the mask.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int togglemiddlebits( int n)
{
if (n < 4)
return n;
int mask = (1 << ( int )log2(n)) - 2;
return mask ^ n;
}
int main()
{
int n = 9;
cout << togglemiddlebits(n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int togglemiddlebits( int n)
{
if (n < 4 )
return n;
int mask = ( 1 << ( int )(Math.log(n) / Math.log( 2 ))) - 2 ;
return mask ^ n;
}
public static void main(String[] args)
{
int n = 9 ;
System.out.println(togglemiddlebits(n));
}
}
|
Python3
import math
def togglemiddlebits(n) :
if n < 4 :
return n
mask = int (( 1 << int (math.log2( 14 ))) - 2 )
return mask ^ n
if __name__ = = "__main__" :
n = 9
print (togglemiddlebits(n))
|
C#
using System;
class GFG {
static int togglemiddlebits( int n)
{
if (n < 4)
return n;
int mask
= (1 << ( int )(Math.Log(n) / Math.Log(2))) - 2;
return mask ^ n;
}
public static void Main( string [] args)
{
int n = 9;
Console.WriteLine(togglemiddlebits(n));
}
}
|
Javascript
function togglemiddlebits(n)
{
if (n < 4)
return n;
let mask = (1 << Math.floor(Math.log2(n))) - 2;
return mask ^ n;
}
let n = 9;
console.log(togglemiddlebits(n));
|
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 neveropen!