fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int hitungNomorBit(int angka, int nomorBit) {
  6. int bit;
  7.  
  8. bit = (angka >> nomorBit) & 1;
  9.  
  10. if (bit == 1) {
  11. return (int)pow(2, nomorBit);
  12. } else {
  13. return -1;
  14. }
  15. }
  16.  
  17. int main() {
  18. int angka = 13;
  19.  
  20. // Tes dengan nomorBit 0, 1, 2
  21. cout << "Bit pada posisi 0: " << hitungNomorBit(angka, 0) << endl;
  22. cout << "Bit pada posisi 1: " << hitungNomorBit(angka, 1) << endl;
  23. cout << "Bit pada posisi 2: " << hitungNomorBit(angka, 2) << endl;
  24.  
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Bit pada posisi 0: 1
Bit pada posisi 1: -1
Bit pada posisi 2: 4