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

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

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

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

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

## PHP

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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : raghavbansal011, jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.