fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class First
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println(hitungNomorBit(13, 0));
  13. System.out.println(hitungNomorBit(13, 1));
  14. System.out.println(hitungNomorBit(13, 2));
  15. System.out.println(hitungNomorBit(13, 3));
  16. }
  17.  
  18. public static Integer hitungNomorBit(int angka, int nomorBit) {
  19. if (nomorBit != 0 && nomorBit != 1) {
  20. return null;
  21. }
  22.  
  23. Integer answer = 0;
  24.  
  25. if (angka == 0) {
  26. return nomorBit == 0 ? 1 : 0;
  27. }
  28.  
  29. while (angka != 0) {
  30. if ((angka & 1) == nomorBit) {
  31. answer += 1;
  32. }
  33.  
  34. angka = angka >>> 1;
  35.  
  36. }
  37.  
  38. return answer;
  39.  
  40. }
  41. }
Success #stdin #stdout 0.1s 54564KB
stdin
Standard input is empty
stdout
1
3
null
null