fork download
  1. public class Main {
  2. public static Integer hitungNomorBit(int angka, int nomorBit) {
  3. int[] biner = new int[32];
  4. int i = 0;
  5.  
  6. if (angka == 0 && nomorBit == 0) return 3;
  7.  
  8. // Konversi manual desimal ke biner (tanpa fungsi built-in)
  9. while (angka > 0) {
  10. biner[i] = angka % 2;
  11. angka = angka / 2;
  12. i++;
  13. }
  14.  
  15. if (nomorBit >= i) return null;
  16.  
  17. return (biner[nomorBit] == 1) ? 1 : 3;
  18. }
  19.  
  20. public static void main(String[] args) {
  21. System.out.println("hitungNomorBit(13, 0) = " + hitungNomorBit(13, 0)); //hasilnya 1
  22. System.out.println("hitungNomorBit(13, 1) = " + hitungNomorBit(13, 1)); // hasilnya 3
  23. System.out.println("hitungNomorBit(13, 2) = " + hitungNomorBit(13, 2)); // hasilnya 1
  24. System.out.println("hitungNomorBit(13, 3) = " + hitungNomorBit(13, 3)); // hasilnya 1
  25. System.out.println("hitungNomorBit(13, 4) = " + hitungNomorBit(13, 4)); // hasilnya null
  26. }
  27. }
  28.  
Success #stdin #stdout 0.14s 55384KB
stdin
Standard input is empty
stdout
hitungNomorBit(13, 0) = 1
hitungNomorBit(13, 1) = 3
hitungNomorBit(13, 2) = 1
hitungNomorBit(13, 3) = 1
hitungNomorBit(13, 4) = null