Toggle bits of a number except first and last bits

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

1) Generate a number which contains middle bit as set. We need to change all middle bits to 1 and keep corner bits as 0.



2) Answer is XOR of generated number and original number. Note that XOR of 1 with a number toggles the number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to toggle bits 
// except first and last bit
#include<iostream>
using namespace std;
  
// return set middle bits
int setmiddlebits(int n)
{
  
    // set all bit
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
  
    // return middle set bits
    // shift by 1 and xor with 1
    return (n >> 1) ^ 1;
}
  
int togglemiddlebits(int n)
{
      
    // if number is 1 then
    // simply return
    if (n == 1)
        return 1;
  
    // xor with
    // middle bits
    return n ^ setmiddlebits(n);
}
  
// Driver Code
int main()
{
      
    // Given number
    int n = 9;
  
    // print toggle bits
    cout<<togglemiddlebits(n);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for toggle bits
// expect first and last bit
import java.io.*;
  
class GFG {
      
    // return set middle bits
    static int setmiddlebits(int n)
    {
  
        // set all bit
        n |= n >> 1;
        n |= n >> 2;
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
      
        // return middle set bits
        // shift by 1 and xor with 1
        return (n >> 1) ^ 1;
    }
  
    static int togglemiddlebits(int n)
    {
        // if number is 1 then 
        // simply return
        if (n == 1)
            return 1;
      
        // XOR with middle bits
        return n ^ setmiddlebits(n);
    }
      
    // Driver Code
    public static void main (String[] args) 
    {
      
        // Given number
        int n = 9;
      
        // print toggle bits
        System.out.println(togglemiddlebits(n));
    }
}
  
// This code is contributed by vt_m
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for toggle bits
# expect first and last bit
  
# return set middle bits
def setmiddlebits(n):
  
    # set all bit
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
  
    # return middle set bits
    # shift by 1 and xor with 1
    return (n >> 1) ^ 1
  
def togglemiddlebits(n):
  
    # if number is 1 then simply return
    if (n == 1):
        return 1
  
    # xor with middle bits
    return n ^ setmiddlebits(n)
  
# Driver code
n = 9
print(togglemiddlebits(n))
  
# This code is contributed by Anant Agarwal.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for toggle bits
// expect first and last bit
using System;
  
class GFG {
      
    // return set middle bits
    static int setmiddlebits(int n)
    {
  
        // set all bit
        n |= n >> 1;
        n |= n >> 2;
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
          
        // return middle set bits
        // shift by 1 and xor with 1
        return (n >> 1) ^ 1;
    }
  
    static int togglemiddlebits(int n)
    {
          
        // if number is 1 then 
        // simply return
        if (n == 1)
            return 1;
      
        // XOR with middle bits
        return n ^ setmiddlebits(n);
    }
      
    // Driver Code
    public static void Main ()
    {
      
        // Given number
        int n = 9;
      
        // print toggle bits
        Console.WriteLine(togglemiddlebits(n));
    }
}
  
// This code is contributed by Anant Agarwal.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Php Program to toggle bits 
// except first and last bit
  
// return set middle bits
function setmiddlebits($n)
{
  
    // set all bit
    $n |= $n >> 1;
    $n |= $n >> 2;
    $n |= $n >> 4;
    $n |= $n >> 8;
    $n |= $n >> 16;
  
    // return middle set bits
    // shift by 1 and xor with 1
    return ($n >> 1) ^ 1;
}
  
function togglemiddlebits($n)
{
      
    // if number is 1 then
    // simply return
    if ($n == 1)
        return 1;
  
    // xor with
    // middle bits
    return $n ^ setmiddlebits($n);
}
  
    // Driver Code
    $n = 9;
  
    // print toggle bits
    echo togglemiddlebits($n);
  
// This code is contributed by ajit
?>
chevron_right

Time Complexity:- O(1)
Output:

15

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.




Article Tags :
Practice Tags :