Toggle bits of a number except first and last bits - GeeksforGeeks

22 Mar, 2021
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 number1) 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. 
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to toggle bits// except first and last bit#include<iostream>using namespace std; // return set middle bitsint 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 Codeint main(){         // Given number    int n = 9;     // print toggle bits    cout<<togglemiddlebits(n);     return 0;}
// Java program for toggle bits// expect first and last bitimport 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
# Python3 program for toggle bits# expect first and last bit # return set middle bitsdef 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 coden = 9print(togglemiddlebits(n)) # This code is contributed by Anant Agarwal.
// C# program for toggle bits// expect first and last bitusing 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.
<?php// Php Program to toggle bits// except first and last bit // return set middle bitsfunction 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?>
<script> // Javascript Program to toggle bits// except first and last bit // return set middle bitsfunction 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 // Given numbervar n = 9;// print toggle bitsdocument.write(togglemiddlebits(n));     </script>
Time Complexity:- O(1) Output: 
15
 
raghavbansal011
jit_t
noob2000
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Binary representation of a given number
Program to find whether a given number is power of 2
Bit Fields in C
1's and 2's complement of a Binary Number
Bits manipulation (Important tactics)
Josephus problem | Set 1 (A O(n) Solution)
Add two numbers without using arithmetic operators
Set, Clear and Toggle a given bit of a number in C