1269. Toggle the Nth Bit of a Number

EasyBit ManipulationBit ManipulationBit MaskingXOR

Given an integer n and a 0-indexed bit position k, return the value obtained by toggling (flipping) the k-th bit of n: a 1 becomes 0 and a 0 becomes 1.

Input: A JSON object {"n": <integer>, "k": <bit index>} with 0 <= k <= 40.

Output: Return the integer n with bit k flipped.

Examples

Example 1
Input: {"n":5,"k":0}
Output: 4
Explanation: 101 ^ 001 = 100 = 4.
Example 2
Input: {"n":5,"k":1}
Output: 7
Explanation: 101 ^ 010 = 111 = 7.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →