Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Problem_7_CountOfBitsOne {
- public static void main(String[] args) {
- int num=15;
- long count=0;
- //First way
- //System.out.println(Integer.bitCount(num));
- //Second way
- String str= Integer.toBinaryString(num);
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) == '1') {
- count++;
- }
- }
- System.out.println(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement