Created
August 18, 2022 16:50
-
-
Save krupitskas/422b86a1f70d9732b890c4eeeae921ea to your computer and use it in GitHub Desktop.
Bit packing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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