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

Appearance settings

Latest commit

 

History

History
History
66 lines (54 loc) · 1.44 KB

File metadata and controls

66 lines (54 loc) · 1.44 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package bit;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class BitOperator {
@Test
public void test() {
assertThat(isPowerOf2(2), is(true));
assertThat(isPowerOf2(4), is(true));
assertThat(isPowerOf2(8), is(true));
assertThat(isPowerOf2(16), is(true));
assertThat(isPowerOf2(17), is(false));
}
public String testFunction() {
return "";
}
public boolean get(int n, int i) {
int mask = 1 << i;
return (n & mask) != 0;
}
public int set(int n, int i) {
int mask = 1 << i;
return n | mask;
}
public int clear(int n, int i) {
int mask = ~(1 << i);
return n & mask;
}
/*
TASK
2의 제곱수인지 판별한다.
*/
public boolean isPowerOf2(int n) {
// 10 == 2 => 2 - 1 == 1
// 100 == 4 => 4 - 1 == 11
// 1000 == 8 => 8 - 1 == 111
// 10000 == 16 => 16 - 1 == 1111
// n에서 1을 뺀다음에 n과 and 연산을 하면 모두 0이 된다.
return (n & (n - 1)) == 0;
}
/*
TASK
두 수에서 다른 비트의 개수를 구한다.
*/
public int getBitDiff(int a, int b) {
int diff = a ^ b; //XOR
int count = 0;
while (diff != 0) {
diff = diff & (diff - 1);
count++;
}
return count;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.