forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwosCompliment.java
36 lines (29 loc) · 848 Bytes
/
TwosCompliment.java
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
package com.hackerrank.bitmanipulation;
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/24/15
* @time: 10:25 PM
*/
public class TwosCompliment {
public static long countSetBitsInRange(int start, int end) {
int count = 0;
for (int i = start; i <= end; i++) {
count += Integer.bitCount(i);
}
return count;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = Integer.parseInt(in.nextLine());
String[][] in_ar = new String[t][2];
for (int i = 0; i < t; i++) {
in_ar[i] = in.nextLine().split(" ");
}
for (String[] i : in_ar) {
System.out.println(countSetBitsInRange(Integer.parseInt(i[0]), Integer.parseInt(i[1])));
}
}
}