Skip to content

Instantly share code, notes, and snippets.

@krupitskas
Created August 18, 2022 16:50
Show Gist options
  • Save krupitskas/422b86a1f70d9732b890c4eeeae921ea to your computer and use it in GitHub Desktop.
Save krupitskas/422b86a1f70d9732b890c4eeeae921ea to your computer and use it in GitHub Desktop.
Bit packing
/*
00000001 10000000 | 00000001 10000001
0000001 0000000 | 0000001 0000001
0000001 0000000 | 00000010000001
128 | 129
*/
#include <cstdint>
#include <cstring>
int main() {
constexpr int size = 2;
char input[size] = {0b00000001, 0b10000000};
int64_t res = 0;
for (int i = 0; i < size; ++i) {
int64_t byte = 0;
std::memcpy(&byte, &input[i], sizeof(char));
const bool eof = byte & (1 << 7);
byte = byte & ~(1 << 7);
res <<= i * 7;
res |= byte;
if (eof)
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment