TCS Coding Questions 2022 Day 1 Slot 1

Coding Question 2 for 2022 (September slot)

In this article, we will discuss about the TCS Coding Question which is asked in the TCS placement test. This type of Coding Questions will help you to crack your upcoming TCS exam as well as during your inteview process.

TCS Coding Question 4 Day1 Slot 1

TCS Coding Question Day 1 Slot 1 – Question 2

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”.


Constraints :

1<=N<=100


Example 1:

Input :

10  -> Integer

Output :

5    -> result- Integer


Explanation:

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

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n; cin>>n;
int k=(1<<(int)floor(log2(n))+1)-1;
cout<<(n^k);
}
import java.util.*;
class Solution
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int no = sc.nextInt ();
String bin = "";

while (no != 0)
{
bin = (no & 1) + bin;
no = no >> 1;
}
bin = bin.replaceAll ("1", "2");
bin = bin.replaceAll ("0", "1");
bin = bin.replaceAll ("2", "0");

int res = Integer.parseInt (bin, 2);
System.out.println (res);
}
}
import math
n=int(input())
k=(1<<int(math.log2(n))+1)-1
print(n^k)