For every set bit of a number toggle bits of other Last Updated : 31 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two integer numbers, whenever the bits of the first number is set i.e. 1, toggle the bits of the second number leaving the rest bits of the second number unchanged. Examples : Input: 2 5 Output: 7 2 is represented as 10 in binary and 5 is represented as 101. Hence toggling the 2nd bit of 5 from right, thus the new number becomes 7 i.e. 111 Input: 1 3 Output: 2 Approach: Just do XOR of the given two Number. C++ // A CPP program toggle bits of n2 // that are at same position as set // bits of n1. #include <bits/stdc++.h> using namespace std; // function for the Nega_bit int toggleBits(int n1, int n2) { return n1 ^ n2; } // Driver program to test above int main() { int n1 = 2, n2 = 5; cout << toggleBitst(n1, n2) << endl; return 0; } Java // A Java program toggle bits of n2 // that are at same position as set // bits of n1. import java.io.*; class GFG { // function for the Nega_bit static int toggleBits(int n1, int n2) { return (n1 ^ n2); } // Driver program public static void main(String args[]) { int n1 = 2, n2 = 5; System.out.println(toggleBits(n1, n2)); } } // This code is contributed // by Nikita Tiwari. Python3 # A Python 3 program toggle bits of n2 # that are at same position as set # bits of n1. # function for the Nega_bit def toggleBits(n1, n2) : return (n1 ^ n2) # Driver program to test above n1 = 2 n2 = 5 print(toggleBits(n1, n2)) # This code is contributed # by Nikita Tiwari. C# // C# program toggle bits of n2 // that are at same position as set // bits of n1. using System; class GFG { // function for the Nega_bit static int toggleBits(int n1, int n2) { return (n1 ^ n2); } // Driver program public static void Main() { int n1 = 2, n2 = 5; Console.WriteLine(toggleBits(n1, n2)); } } // This code is contributed by Anant Agarwal. PHP <?php // PHP program to toggle bits of n2 // that are at same position as set // bits of n1. // function for the Nega_bit function toggleBits($n1, $n2) { return $n1 ^ $n2; } // Driver code $n1 = 2; $n2 = 5; echo toggleBits($n1, $n2)."\n"; // This code is contributed by mits ?> JavaScript <script> // JavaScript program toggle bits of n2 // that are at same position as set // bits of n1 // Function for the Nega_bit function toggleBits(n1, n2) { return (n1 ^ n2); } // Driver code let n1 = 2, n2 = 5; document.write(toggleBits(n1, n2)); // This code is contributed by sanjoy_62 </script> Output : 7 Time Complexity : O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Toggle all even bits of a number A ajay0007 Follow Improve Article Tags : Bit Magic Technical Scripter DSA Bitwise-XOR Practice Tags : Bit Magic Similar Reads 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 Program to toggle K-th bit of a number N Given a number N, the task is to clear the K-th bit of this number N. If K-th bit is 0, then set it to 1 and if it is 1 then set it to 0. Examples: Input: N = 5, K = 2 Output: 7 5 is represented as 101 in binary and has its second bit 0, so toggling it will result in 111 i.e. 7. Input: N = 5, K = 1 3 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 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 Prerequisi 8 min read Set all the bits in given range of a number Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit. Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex 5 min read Set the K-th bit of a given number Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or last bit) is 0, second last bit is 1 and so on. Also, 0 <= k < x, where x is the number of bits in the binary representation of n.Examples: Input : n = 10, k = 2 Output : 1 4 min read Like