MCPcopy Create free account
hub / github.com/careercup/ctci / findOpenNumber

Method findOpenNumber

java/Chapter 10/Question10_3/Question.java:12–33  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10 public static byte[] bitfield = new byte [(int) (numberOfInts / 8)];
11
12 public static void findOpenNumber() throws FileNotFoundException {
13 Scanner in = new Scanner(new FileReader("Chapter 10/Question10_3/input_file_q10_3.txt"));
14 while (in.hasNextInt()) {
15 int n = in.nextInt ();
16 /* Finds the corresponding number in the bitfield by using
17 * the OR operator to set the nth bit of a byte
18 * (e.g., 10 would correspond to the 2nd bit of index 2 in
19 * the byte array). */
20 bitfield [n / 8] |= 1 << (n % 8);
21 }
22
23 for (int i = 0; i < bitfield.length; i++) {
24 for (int j = 0; j < 8; j++) {
25 /* Retrieves the individual bits of each byte. When 0 bit
26 * is found, finds the corresponding value. */
27 if ((bitfield[i] & (1 << j)) == 0) {
28 System.out.println (i * 8 + j);
29 return;
30 }
31 }
32 }
33 }
34
35 public static void main(String[] args) throws IOException {
36 findOpenNumber();

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected