fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class BitArr
  5. {
  6. public:
  7. unsigned char * data;
  8. BitArr(int);
  9. bool getValue(int);
  10. void setValue(int, bool);
  11. ~BitArr();
  12. };
  13.  
  14. BitArr::BitArr(int s)
  15. {
  16. int size = s/8;
  17. if(s%8!=0)
  18. size++;
  19.  
  20. this->data = (unsigned char*)calloc(size, sizeof(unsigned char));
  21. }
  22.  
  23. BitArr::~BitArr()
  24. {
  25. free(this->data);
  26. }
  27.  
  28. bool BitArr::getValue(int pos)
  29. {
  30. return (*(this->data+pos/8) & (1<<(8-pos%8)));
  31. }
  32.  
  33. void BitArr::setValue(int pos, bool val)
  34. {
  35. if(val == true)
  36. *(this->data+pos/8) |= (1<<(8-pos%8));
  37. else
  38. *(this->data+pos/8) &= ~(1<<(8-pos%8));
  39. return;
  40. }
  41.  
  42. int main() {
  43. BitArr a(20);
  44. a.setValue(3, true);
  45. a.setValue(7, true);
  46. a.setValue(18, true);
  47. for(int i = 0 ; i<20; i++)
  48. cout << a.getValue(i);
  49. return 0;
  50. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
00010001000000000010