GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithm_basic_java/src/test/java/bit/BitOperator.java at master · jbee37142/algorithm_basic_java

Expand file tree

/

BitOperator.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
66 lines (54 loc) · 1.44 KB
/

BitOperator.java

Copy path

File metadata and controls

66 lines (54 loc) · 1.44 KB
Raw
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;
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.