 # TCS NQT TOGGLE BITS CODING PROBLEM WITH SOLUTION

Joseph is learning digital logic subject which will be for his next semester. He usually tries to solve unit assignment problems before the lecture. Today he got one tricky question. The problem statement is “A positive integer has been given as an input. Convert decimal value to binary representation. Toggle all bits of it after the most significant bit including the most significant bit. Print the positive integer value after toggling all bits”.

**Input format**:

– First line: An positive integer N.

**Output format**:

– First line: An integer M resulting from processing N as described in the problem statement.

**Constraints:**

“`

1 <= N <= 100

“`

**Examples**

– Input: 10

– Output: 5

– Explanation: Binary representation of 10 is 1010. After toggling the bits(1010), will get 0101 which represents “5”. Hence output will print “5”.

Program in C

``````#include <stdio.h>
#include <math.h>
int main() {
unsigned int n;
scanf("%ud", &n);
int xor_op = (1 << (int) floor (log2(n)) + 1) - 1;
printf("%d", n ^ xor_op);
return 0;
}
``````

Program in C++

``````#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned int n;
cin >> n;
int xor_op = (1 << static_cast<int> (floor (log2(n) + 1))) - 1;
printf("%d", n ^ xor_op);
return 0;
}
``````

Program in Java

``````import java.util.Scanner;
class Main {
public static int log2(int N)
{
return (int)(Math.log(N) / Math.log(2));
}
public static void main (String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.close();
int xor_op = (1 << (int) Math.floor (log2(n) + 1)) - 1;
System.out.print(n ^ xor_op);
}
}
``````

Program in Python

``````if __name__ == '__main__':
n = int(input())
import math
xor_op = (1 << int(math.log2(n) + 1)) - 1
print(n ^ xor_op, end = '')
``````