-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathposition.java
More file actions
63 lines (51 loc) · 1.5 KB
/
position.java
File metadata and controls
63 lines (51 loc) · 1.5 KB
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
package com.ub.codeeval;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class position {
private boolean isSameBitPosition(int number,int pos1,int pos2) {
pos1 = pos1 - 1;
pos2 = pos2 - 1;
int p1 = 1 << pos1;
int p2 = 1 << pos2;
p1 = (((number & p1) >> pos1) ^ ((number & p2) >> pos2));
if (p1 == 0)
return true;
else
return false;
}
public static void main(String args[]) {
position bit = new position();
try {
File file = new File(args[0]);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
try {
while ((line = in.readLine()) != null) {
String[] strs = line.split(",");
int number = Integer.parseInt(strs[0]);
int pos1 = Integer.parseInt(strs[1]);
int pos2 = Integer.parseInt(strs[2]);
boolean isAns = bit.isSameBitPosition(number,pos1,pos2);
// isAns = bit.isSameBitPosition(125,1,2);
if(isAns == true) {
System.out.println("true");
}else {
System.out.println("false");
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}