fork download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. function hitungNomorBit(angka, nomorBit) {
  5. if (nomorBit !== 0 && nomorBit !== 1) {
  6. return null;
  7. }
  8.  
  9. if (angka === 0) {
  10. return nomorBit === 0 ? 1 : 0;
  11. }
  12.  
  13. let jumlah = 0;
  14. let sisa = angka;
  15.  
  16. while (sisa > 0) {
  17. const digit = sisa % 2;
  18. if (digit === nomorBit) {
  19. jumlah++;
  20. }
  21. sisa = (sisa - digit) / 2;
  22. }
  23.  
  24. return jumlah;
  25. }
  26.  
  27. console.log(hitungNomorBit(13, 0));
  28. console.log(hitungNomorBit(13, 1));
  29. console.log(hitungNomorBit(13, 2));
  30.  
Success #stdin #stdout 0.05s 44632KB
stdin
Standard input is empty
stdout
1
3
null