fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. uint32_t bits = 0;
  5. uint8_t index = 0;
  6. uint8_t maxDigits = 10;
  7.  
  8. char test[] = "10010111#";
  9.  
  10. int main()
  11. {
  12. char * p = test;
  13. char c = '\0';
  14. bool validate = false;
  15.  
  16. while ((c = *p++) != '\0')
  17. {
  18. switch (c)
  19. {
  20. case '0':
  21. case '1':
  22. {
  23. if (index < maxDigits)
  24. {
  25. bits = bits | (c - '0') << index++;
  26. }
  27. else
  28. {
  29. validate = true;
  30. }
  31. break;
  32. }
  33. case '#':
  34. {
  35. validate = true;
  36. break;
  37. }
  38. }
  39.  
  40. if (validate == true)
  41. {
  42. printf("bits = %d\n", bits);
  43. for (uint8_t i = 0; i < index; i++)
  44. {
  45. printf("%d %s\n", i, (bits >> i) & 1 != 0 ? ("true") : ("false"));
  46. }
  47. bits = 0;
  48. index = 0;
  49. validate = false;
  50. break;
  51. }
  52. }
  53. return 0;
  54. }
Success #stdin #stdout 0s 4560KB
stdin
Standard input is empty
stdout
bits = 233
0 true
1 false
2 false
3 true
4 false
5 true
6 true
7 true