fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. char testchar = 0x08;
  6.  
  7. int bitsSet = 0;
  8. int bitNum = 0;
  9. int i = 1;
  10.  
  11. for (unsigned char c = 0x01; c != 0x00; c = c << 1)
  12. {
  13. if (c & testchar)
  14. {
  15. bitsSet++;
  16. bitNum = i;
  17. }
  18.  
  19. i++;
  20. }
  21. cout << "Bits set [" << bitsSet << "] bitNum [" << bitNum << "]" << endl;
  22. return 0;
  23. }
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
Bits set [1] bitNum [4]