fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. unsigned char encodeBits(bool bools[8]);
  6. void decodeBits(bool (&bools)[8], unsigned char input);
  7.  
  8. int main() {
  9. bool in[] = { true, false, false, true, true, false, false, true };
  10. bool out[8] = { false, false, false, false, false, false, false, false };
  11.  
  12. unsigned char temp = encodeBits(in);
  13.  
  14. cout << (int)temp << endl;
  15.  
  16. decodeBits(out, temp);
  17.  
  18. for(int i = 0; i < 8; i++) {
  19. if(out[i]) {
  20. cout << "True, ";
  21. } else {
  22. cout << "False, ";
  23. }
  24. }
  25.  
  26. cout << endl;
  27.  
  28. return 0;
  29. }
  30.  
  31. unsigned char encodeBits(bool bools[8]) {
  32. unsigned char retval = 0;
  33. for(int i = 0; i < 8; i++) {
  34. retval += (bools[i] ? 1 : 0);
  35. retval = (i < 7 ? retval << 1 : retval);
  36. }
  37.  
  38. return retval;
  39. }
  40.  
  41. void decodeBits(bool (&bools)[8], unsigned char input) {
  42. for(int i = 0; i < 8; i++) {
  43. bools[i] = (input & 0x00000001 ? true : false);
  44. input = (i < 7 ? input >> 1 : input);
  45. }
  46. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
153
True, False, False, True, True, False, False, True,