Toggle all odd bits of a number Last Updated : 27 May, 2022 Comments Improve Suggest changes Like Article Like Report Given n number, the task is to toggle odd bit of the number.Examples: Input : 10 Output : 15 binary representation 1 0 1 0 after toggle 1 1 1 1 Input : 20 Output : 1 binary representation 1 0 1 0 0 after toggle 0 0 0 0 1 1. First generate a number that contains odd position bits. 2. Take XOR with the original number. Note that 1 ^ 1 = 0 and 1 ^ 0 = 1.Let’s understand this approach with below code. C++ // Toggle all odd bit of a number #include <iostream> using namespace std; // Returns a number which has all odd // bits of n toggled. int evenbittogglenumber(int n) { // Generate number form of 101010... // ..till of same order as n int res = 0, count = 0; for (int temp = n; temp > 0; temp >>= 1) { // if bit is odd, then generate // number and or with res if (count % 2 == 0) res |= (1 << count); count++; } // return toggled number return n ^ res; } // Driver code int main() { int n = 11; cout << evenbittogglenumber(n); return 0; } Java // Toggle all odd bit of a number import java.io.*; class GFG { // Returns a number which has all odd // bits of n toggled. static int evenbittogglenumber(int n) { // Generate number form of 101010... // ..till of same order as n int res = 0, count = 0; for (int temp = n; temp > 0; temp >>= 1) { // if bit is odd, then generate // number and or with res if (count % 2 == 0) res |= (1 << count); count++; } // return toggled number return n ^ res; } // Driver code public static void main(String args[]) { int n = 11; System.out.println(evenbittogglenumber(n)); } } /*This code is contributed by Nikita tiwari.*/ Python3 # Python3 code for Toggle all odd bit of a number # Returns a number which has all odd # bits of n toggled. def evenbittogglenumber(n) : # Generate number form of 101010... # ..till of same order as n res = 0; count = 0; temp = n while(temp > 0 ) : # If bit is odd, then generate # number and or with res if (count % 2 == 0) : res = res | (1 << count) count = count + 1 temp >>= 1 # Return toggled number return n ^ res # Driver code if __name__ == '__main__' : n = 11 print(evenbittogglenumber(n)) # This code is contributed by Nikita Tiwari. C# // C# code for Toggle all odd bit of a number using System; class GFG { // Returns a number which has all odd // bits of n toggled. static int evenbittogglenumber(int n) { // Generate number form of 101010... // ..till of same order as n int res = 0, count = 0; for (int temp = n; temp > 0; temp >>= 1) { // if bit is odd, then generate // number and or with res if (count % 2 == 0) res |= (1 << count); count++; } // return toggled number return n ^ res; } // Driver code public static void Main() { int n = 11; Console.WriteLine(evenbittogglenumber(n)); } } // This code is contributed by Anant Agarwal. PHP <?php // php implementation of Toggle // all odd bit of a number // Returns a number which has // all odd bits of n toggled. function evenbittogglenumber($n) { // Generate number form of 101010... // ..till of same order as n $res = 0; $count = 0; for ($temp = $n; $temp > 0; $temp >>= 1) { // if bit is odd, then generate // number and or with res if ($count % 2 == 0) $res |= (1 << $count); $count++; } // return toggled number return $n ^ $res; } // Driver code $n = 11; echo evenbittogglenumber($n); // This code is contributed by mits ?> JavaScript <script> // JavaScript program Toggle all odd bit of a number // Returns a number which has all odd // bits of n toggled. function evenbittogglenumber(n) { // Generate number form of 101010... // ..till of same order as n let res = 0, count = 0; for (let temp = n; temp > 0; temp >>= 1) { // if bit is odd, then generate // number and or with res if (count % 2 == 0) res |= (1 << count); count++; } // return toggled number return n ^ res; } // Driver code let n = 11; document.write(evenbittogglenumber(n)); </script> Output : 14 Time Complexity : O(log n) Space Complexity : O(1) Comment More infoAdvertise with us Next Article Toggle all odd bits of a number D devanshuagarwal Follow Improve Article Tags : Misc Bit Magic DSA Practice Tags : Bit MagicMisc Similar Reads Set all odd bits of a number Given a number, the task is to set all odd bits of a number. Positions of bits are counted from LSB (least significant bit) to MSB (Most significant bit). Position of LSB is considered as 1. Examples : Input : 20 Output : 21 Explanation : Binary representation of 20 is 10100. Setting all odd bits ma 15+ min read Toggle all even bits of a number Given a number, the task is to Toggle all even bit of a numberExamples: Input : 10 Output : 0 binary representation 1 0 1 0 after toggle 0 0 0 0 Input : 20 Output : 30 binary representation 1 0 1 0 0 after toggle 1 1 1 1 0 1. First generate a number that contains even position bits. 2. Take XOR with 8 min read Set all even bits of a number Given a number, the task is to set all even bits of a number. Positions of bits are counted from LSB (least significant bit) to MSB (Most significant bit). The position of LSB is considered as 1. Examples : Input : 20 Output : 30 Binary representation of 20 is 10100. After setting even bits, we get 14 min read Toggle all the bits of a number except k-th bit. Given a positive (or unsigned) integer n, write a function to toggle all the bits except k-th bit. Here value of k starts from 0 (zero) and from right. Examples: Input : n = 4294967295, k = 0 Output : 1 The number 4294967295 in 32 bits has all bits set. When we toggle all bits except last bit, we ge 4 min read Bitwise AND of all the odd numbers from 1 to N Given an integer N, the task is to find the bitwise AND (&) of all the odd integers from the range [1, N]. Examples: Input: N = 7 Output: 1 (1 & 3 & 5 & 7) = 1 Input: N = 1 Output: 1 Naive approach: Starting from 1, bitwise AND all the odd numbers ? N. Below is the implementation of 5 min read Like