File tree Expand file tree Collapse file tree 6 files changed +68
-18
lines changed Expand file tree Collapse file tree 6 files changed +68
-18
lines changed Original file line number Diff line number Diff line change 4444 </ItemGroup >
4545 <ItemGroup >
4646 <Compile Include =" HammingDistanceSln.cs" />
47+ <Compile Include =" NumberComplementSln.cs" />
4748 <Compile Include =" PowOfFourSln.cs" />
4849 <Compile Include =" Program.cs" />
4950 <Compile Include =" Properties\AssemblyInfo.cs" />
Original file line number Diff line number Diff line change 1+ /* ==============================================================================
2+ * 功能描述:NumberComplementSln
3+ * 创 建 者:gz
4+ * 创建日期:2017/6/2 13:48:57
5+ * ==============================================================================*/
6+ using System ;
7+ using System . Collections . Generic ;
8+ using System . Diagnostics . CodeAnalysis ;
9+ using System . Linq ;
10+ using System . Text ;
11+
12+ namespace BitManipulation
13+ {
14+ /// <summary>
15+ ///#476 NumberComplementSln
16+ /// </summary>
17+ public class NumberComplementSln
18+ {
19+ public int FindComplement ( int num )
20+ {
21+ int bits = 1 ; //num including bits
22+ while ( Math . Pow ( 2 , bits ) <= num )
23+ bits ++ ;
24+ int sum = ( int ) Math . Pow ( 2 , bits ) - 1 ; //sum =Pow(2,n)-1: sum of n bits 1
25+ return sum - num ; //sum - num is the complement
26+
27+ }
28+
29+ }
30+ }
Original file line number Diff line number Diff line change @@ -9,8 +9,8 @@ class Program
99 {
1010 static void Main ( string [ ] args )
1111 {
12- HammingDistanceSln sln = new HammingDistanceSln ( ) ;
13- int d = sln . HammingDistance ( 1 , 8 ) ;
12+ NumberComplementSln sln = new NumberComplementSln ( ) ;
13+ int n = sln . FindComplement ( 6 ) ;
1414 }
1515 }
1616}
Original file line number Diff line number Diff line change 33https://github.com/jackzhenguo/LeetCodeManager
44
55## Bit Mainpulation
6- ### 461 Hamming Distance
7- * [ CSDN:#461 Hamming Distance] ( http://blog.csdn.net/daigualu/article/details/72830624 )
8- > x&(x-1) application; xor application</br >
6+ * [ CSDN:#476 Number Complement] ( http://blog.csdn.net/daigualu/article/details/72843822 )
7+ > get bits for a number</br >
98 ``` C#
10- public int HammingDistance (int x , int y )
9+ public int FindComplement (int num )
1110 {
12- /// a and y are different bits, so we think the XOR
13- /// think:0001(1D)
14- /// 0100(4D)
15- /// xor = 0101(1D^4D)
16- int dist = 0 , xor = x ^ y ;
17- while (xor > 0 )
18- {
19- /// xor & (xor-1): it sets the rightest 1 bit to 0 bit of xor.
20- ++ dist ;
21- xor = xor & (xor - 1 );
22- }
23- return dist ;
11+ int bits = 1 ; // num including bits
12+ while (Math .Pow (2 , bits ) <= num )
13+ bits ++ ;
14+ int sum = (int ) Math .Pow (2 , bits ) - 1 ;// sum =Pow(2,n)-1: sum of n bits 1
15+ return sum - num ; // sum - num is the complement
16+
2417 }
2518 ```C #
Original file line number Diff line number Diff line change 9292### 342 Power of Four
9393* [ Github:#342 Power of Four] ( /BitManipulation/PowOfFourSln.cs )
9494* [ CSDN:#342 Power of Four] ( http://blog.csdn.net/daigualu/article/details/72821233 )
95+
96+ # ` Today Update `
97+ (Notes: "&hearts ; " Welcome to visit or fork or star my LeetCode Manager @
98+ https://github.com/jackzhenguo/LeetCodeManager
99+
100+ ## Bit Mainpulation
101+ ### 461 Hamming Distance
102+ * [ CSDN:#461 Hamming Distance] ( http://blog.csdn.net/daigualu/article/details/72830624 )
103+ > x&(x-1) application; xor application</br >
104+ ``` C#
105+ public int HammingDistance (int x , int y )
106+ {
107+ /// a and y are different bits, so we think the XOR
108+ /// think:0001(1D)
109+ /// 0100(4D)
110+ /// xor = 0101(1D^4D)
111+ int dist = 0 , xor = x ^ y ;
112+ while (xor > 0 )
113+ {
114+ /// xor & (xor-1): it sets the rightest 1 bit to 0 bit of xor.
115+ ++ dist ;
116+ xor = xor & (xor - 1 );
117+ }
118+ return dist ;
119+ }
120+ ```C #
You can’t perform that action at this time.
0 commit comments