@RyanMarcus/

bitpack

C++11

No description

fork
loading
Files
  • main.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

int main() {
  int16_t y = 32;
  int8_t z = 100;
  uint8_t q = 128;
  int32_t w = 334281;

  uint64_t packed = 0;

  packed |= ((1 << 16) - 1) & y;

  packed <<= 8;
  packed |= ((1 << 8) - 1) & z;

  packed <<= 8;
  packed |= ((1 << 8) - 1) & q;

  packed <<= 32;
  packed |= (((int64_t)1 << 32) - 1) & w;


  // unpack the values in reverse order
  int64_t rec = (((int64_t)1 << 32) - 1) & packed;
  std::cout << rec << std::endl;
  packed >>= 32;

  rec = (((int64_t)1 << 8) - 1) & packed;
  std::cout << rec << std::endl;
  packed >>= 8;

  rec = (((int64_t)1 << 8) - 1) & packed;
  std::cout << rec << std::endl;
  packed >>= 8;

  rec = (((int64_t)1 << 16) - 1) & packed;
  std::cout << rec << std::endl;
  packed >>= 16;




}