-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path4_Question.cpp
More file actions
58 lines (46 loc) · 1.3 KB
/
4_Question.cpp
File metadata and controls
58 lines (46 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Problem Statement –
// 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”.
// Constrains-
// 1<=N<=100
// Example 1:
// Input : 10 -> Integer (1010)
// Output : // 5 -> result- Integer (0101)
// Explanation:
// Binary representation of 10 is 1010. After toggling the bits(1010), will get 0101 which represents “5”.
// Hence output will print “5”.
// 1
// #include<bits/stdc++.h>
// using namespace std;
// int ToggleBits(int n){
// int no_of_bits = floor(log2(n)) + 1;
// int k = (1 << no_of_bits) - 1;
// return k^n;
// }
// int main(){
// int n;
// cin>>n;
// cout<<ToggleBits(n);
// return 0;
// }
// 2
#include<bits/stdc++.h>
using namespace std;
void ToggleBits(int &n){
int temp = 1;
while (temp <= n)
{
n ^= temp;
temp <<= 1;
}
}
int main(){
int n;
cin>>n;
ToggleBits(n);
cout<<n;
return 0;
}