Skip to content

Commit bb909aa

Browse files
committed
new 1 bit copy from jdk
1 parent 2b1c3d2 commit bb909aa

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

number-of-1-bits/README.md

Whitespace-only changes.

number-of-1-bits/Solution.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
// copy from JDK Integer.bitCount
3+
/**
4+
* Returns the number of one-bits in the two's complement binary
5+
* representation of the specified {@code int} value. This function is
6+
* sometimes referred to as the <i>population count</i>.
7+
*
8+
* @param i the value whose bits are to be counted
9+
* @return the number of one-bits in the two's complement binary
10+
* representation of the specified {@code int} value.
11+
* @since 1.5
12+
*/
13+
public static int bitCount(int i) {
14+
// HD, Figure 5-2
15+
i = i - ((i >>> 1) & 0x55555555);
16+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
17+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
18+
i = i + (i >>> 8);
19+
i = i + (i >>> 16);
20+
return i & 0x3f;
21+
}
22+
23+
// you need to treat n as an unsigned value
24+
public int hammingWeight(int n) {
25+
return bitCount(n);
26+
}
27+
}

number-of-1-bits/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
layout: solution
3+
title: Number of 1 Bits
4+
date: 2015-03-10 12:29:58+08:00
5+
leetcode_id: 191
6+
---
7+
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
8+
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}
9+
{% include {{leetcode_readme}} %}

0 commit comments

Comments
 (0)