fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. vector<int> decimalToBiner(int angka){
  7. vector<int> biner;
  8.  
  9. if(angka == 0){
  10. biner.push_back(0);
  11. return biner;
  12. }
  13.  
  14. while(angka > 0){
  15. biner.push_back(angka%2);
  16. angka /= 2;
  17. }
  18.  
  19. reverse(biner.begin(), biner.end());
  20. return biner;
  21. }
  22.  
  23. int hitungNomorBit(int angka, int nomorBit){
  24. if(nomorBit != 1 && nomorBit != 0){
  25. return -1;
  26. }
  27.  
  28. vector<int> biner = decimalToBiner(angka);
  29.  
  30. int count = 0;
  31. for(int digit : biner){
  32. if(digit == nomorBit){
  33. count++;
  34. }
  35. }
  36.  
  37. return count;
  38. }
  39.  
  40. void tc(int angka, int nomorBit){
  41. int count = hitungNomorBit(angka, nomorBit);
  42. if (count == -1) cout << "null\n";
  43. else cout << count << "\n";
  44. }
  45.  
  46. int main() {
  47. // int t; cin >> t;
  48. // while(t--){
  49. // solve();
  50. // }
  51.  
  52. tc(13, 0);
  53. tc(13, 1);
  54. tc(13, 2);
  55. return 0;
  56. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
1
3
null