# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

 `// C++ Program to toggle bits  ` `// except first and last bit ` `#include ` `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<

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

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

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

 `> 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 ` `?> `

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 :