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