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 33namespace algorithm_exercises_csharp . hackerrank . interview_preparation_kit . miscellaneous ;
44
55using System . Diagnostics . CodeAnalysis ;
6+ using System . Text ;
67
78public static class FlippingBits
89{
910 public static long flippingBits ( long n )
1011 {
1112 string n_bin_str = Convert . ToString ( n , 2 ) ;
1213 n_bin_str = n_bin_str . PadLeft ( 32 , '0' ) ; // Ensure 32 bits
13- string result_bin_str = "" ;
14+ StringBuilder result_bin_str = new StringBuilder ( ) ;
1415
1516 foreach ( char bin_digit in n_bin_str )
1617 {
1718 if ( bin_digit == '1' )
1819 {
19- result_bin_str = result_bin_str + '0' ;
20+ result_bin_str . Append ( '0' ) ;
2021 }
2122 else
2223 {
23- result_bin_str = result_bin_str + '1' ;
24+ result_bin_str . Append ( '1' ) ;
2425 }
2526 }
2627
27- long number = Convert . ToUInt32 ( result_bin_str , 2 ) ;
28+ long number = Convert . ToUInt32 ( result_bin_str . ToString ( ) , 2 ) ;
2829
2930 return number ;
3031 }
You can’t perform that action at this time.
0 commit comments