File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
complement-of-base-10-integer Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ module ;
2
+
3
+ export module complement_of_base_10_integer.Solution;
4
+
5
+ namespace complement_of_base_10_integer
6
+ {
7
+
8
+ export class Solution
9
+ {
10
+ public:
11
+ int bitwiseComplement (int num)
12
+ {
13
+ if (num == 0 )
14
+ return 1 ;
15
+ int s = -1 ;
16
+ for (int i = 31 ; i >= 0 ; i--)
17
+ {
18
+ if (((num >> i) & 1 ) != 0 )
19
+ {
20
+ s = i;
21
+ break ;
22
+ }
23
+ }
24
+ int ans = 0 ;
25
+ for (int i = 0 ; i < s; i++)
26
+ {
27
+ if (((num >> i) & 1 ) == 0 )
28
+ ans |= (1 << i);
29
+ }
30
+ return ans;
31
+ }
32
+ };
33
+ }
Original file line number Diff line number Diff line change
1
+ module ;
2
+
3
+ export module number_complement.Solution;
4
+
5
+ namespace number_complement
6
+ {
7
+
8
+ export class Solution
9
+ {
10
+ public:
11
+ int findComplement (int num)
12
+ {
13
+ if (num == 0 )
14
+ return 1 ;
15
+ int s = -1 ;
16
+ for (int i = 31 ; i >= 0 ; i--)
17
+ {
18
+ if (((num >> i) & 1 ) != 0 )
19
+ {
20
+ s = i;
21
+ break ;
22
+ }
23
+ }
24
+ int ans = 0 ;
25
+ for (int i = 0 ; i < s; i++)
26
+ {
27
+ if (((num >> i) & 1 ) == 0 )
28
+ ans |= (1 << i);
29
+ }
30
+ return ans;
31
+ }
32
+ };
33
+ }
You can’t perform that action at this time.
0 commit comments