fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct encoder {
  5. uint32_t val;
  6.  
  7. encoder(uint32_t _val = 0) : val(_val) {}
  8.  
  9. uint32_t first(uint32_t v = 0) {
  10. if (!v) return (val << (8*3)) >> (8*3);
  11.  
  12. val |= v;
  13. }
  14.  
  15. uint32_t second(uint32_t v = 0) {
  16. if (!v) return (val << (8*2)) >> (8*3);
  17.  
  18. encoder _backupval(val);
  19.  
  20. val = (val >> (8*1));
  21. val |= v;
  22. val = (val << (8*1));
  23. val |= _backupval.first();
  24. return val;
  25. }
  26.  
  27. uint32_t third(uint32_t v = 0) {
  28. if (!v) return (val << (8*1)) >> (8*3);
  29.  
  30. encoder _backupval(val);
  31.  
  32. val = (val >> (8*2));
  33. val |= v;
  34. //now restore
  35. val = val << 8;
  36. val |= _backupval.second();
  37. val = val << 8;
  38. val |= _backupval.first();
  39. return val;
  40. }
  41.  
  42. uint32_t fourth(uint32_t v = 0) {
  43. if (!v) return (val << (8*0)) >> (8*3);
  44.  
  45. encoder _backupval(val);
  46.  
  47. val = (val >> (8*3));
  48. val |= v;
  49. //now restore
  50. val = val << 8;
  51. val |= _backupval.third();
  52. val = val << 8;
  53. val |= _backupval.second();
  54. val = val << 8;
  55. val |= _backupval.first();
  56. }
  57. };
  58.  
  59. int main() {
  60. encoder t;
  61. t.first(6);
  62. t.second(42);
  63. t.third(212);
  64. t.fourth(23);
  65.  
  66. cout << "first number: " << t.first()
  67. << "\nsecond number: " << t.second()
  68. << "\nthird number: " << t.third()
  69. << "\nfourth number: " << t.fourth() << endl;
  70.  
  71. }
  72.  
Success #stdin #stdout 0.02s 2680KB
stdin
Standard input is empty
stdout
first number: 6
second number: 42
third number: 212
fourth number: 23