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.

## CPP

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output :**

7

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.

## Recommended Posts:

- Toggle all the bits of a number except k-th bit.
- Toggle bits of a number except first and last bits
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Toggle all bits after most significant bit
- Set, Clear and Toggle a given bit of a number in C
- Program to toggle K-th bit of a number N
- Toggle first and last bits of a number
- Toggle all even bits of a number
- Toggle all odd bits of a number
- Toggle the last m bits
- Toggle bits in the given range
- Number formed by flipping all bits to the left of rightmost set bit
- Check if bits of a number has count of consecutive set bits in increasing order
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Print numbers having first and last bits as the only set bits
- Check if two numbers are bit rotations of each other or not
- Check if bits in range L to R of two numbers are complement of each other or not
- Inserting m into n such that m starts at bit j and ends at bit i.
- Check if all bits can be made same by flipping two consecutive bits
- Toggle case of a string using Bitwise Operators

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.