-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBit_Flipping.cpp
More file actions
50 lines (42 loc) · 773 Bytes
/
Bit_Flipping.cpp
File metadata and controls
50 lines (42 loc) · 773 Bytes
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
// Bit Flipping
/*
Given an integer A.
Write binary representation of the integer without leading zeros.Flip all bits then return the integer value of the binary number formed.
Flipping means 0 -> 1 and 1 -> 0.
Problem Constraints
1 <= A <= 109
Example Input
Input 1:
A = 7
Input 2:
A = 5
Example Output
Output 1:
0
Output 2:
2
Example Explanation
Explanation 1:
7 -> 111 -> 000 ->0
Explanation 2:
5 -> 101 -> 010 ->2
*/
//Solution in CPP
int Solution::solve(int A)
{
int i = 0;
while ((A >> i) > 0)
{
A = A ^ (1 << i);
i++;
}
return A;
}
/*
Bonous Tips
Binary to Decimal
string binaryNumber = "100101";
int a = stoi(binaryNumber, 0, 2);
Decimal To Binary
string binary = bitset<8>(a).to_string();
*/