Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5a1c2aa

Browse files
update
1 parent c443555 commit 5a1c2aa

File tree

6 files changed

+68
-18
lines changed

6 files changed

+68
-18
lines changed

‎BitManipulation/BitManipulation.suo

512 Bytes
Binary file not shown.

‎BitManipulation/BitManipulation/BitManipulation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
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" />
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

‎BitManipulation/BitManipulation/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

‎TodayUpdate.md

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@
33
https://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, inty)
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#

‎UpdateHistory.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,29 @@
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#

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /