File tree Expand file tree Collapse file tree 1 file changed +5
-4
lines changed
src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous Expand file tree Collapse file tree 1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change 3
3
namespace algorithm_exercises_csharp . hackerrank . interview_preparation_kit . miscellaneous ;
4
4
5
5
using System . Diagnostics . CodeAnalysis ;
6
+ using System . Text ;
6
7
7
8
public static class FlippingBits
8
9
{
9
10
public static long flippingBits ( long n )
10
11
{
11
12
string n_bin_str = Convert . ToString ( n , 2 ) ;
12
13
n_bin_str = n_bin_str . PadLeft ( 32 , '0' ) ; // Ensure 32 bits
13
- string result_bin_str = "" ;
14
+ StringBuilder result_bin_str = new StringBuilder ( ) ;
14
15
15
16
foreach ( char bin_digit in n_bin_str )
16
17
{
17
18
if ( bin_digit == '1' )
18
19
{
19
- result_bin_str = result_bin_str + '0' ;
20
+ result_bin_str . Append ( '0' ) ;
20
21
}
21
22
else
22
23
{
23
- result_bin_str = result_bin_str + '1' ;
24
+ result_bin_str . Append ( '1' ) ;
24
25
}
25
26
}
26
27
27
- long number = Convert . ToUInt32 ( result_bin_str , 2 ) ;
28
+ long number = Convert . ToUInt32 ( result_bin_str . ToString ( ) , 2 ) ;
28
29
29
30
return number ;
30
31
}
You can’t perform that action at this time.
0 commit comments