Last active
April 1, 2017 17:25
-
-
Save maximeborges/2b9e1dbc86d67ea6accaf90a45312ed3 to your computer and use it in GitHub Desktop.
Bitset without padding
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
#include <bitset> | |
#include <cstdint> | |
#include <iostream> | |
using namespace std; | |
struct __attribute__((packed)) Data { | |
uint32_t timestamp : 32; | |
uint32_t gpsLat : 24; | |
uint32_t gpsLong : 24; | |
uint32_t bit0 : 1; | |
uint32_t batteryLevel : 8; | |
uint32_t bit1 : 1; | |
uint32_t bit2 : 1; | |
uint32_t bit3 : 1; | |
uint32_t bit4 : 1; | |
} data; | |
uint8_t *dataBuffer = (uint8_t *)&data; | |
int main() { | |
data.timestamp = -1; | |
data.gpsLat = -1; | |
data.gpsLong = -1; | |
data.bit0 = 0; | |
data.batteryLevel = -1; | |
data.bit1 = 0; | |
data.bit2 = 1; | |
data.bit3 = 0; | |
data.bit4 = 1; | |
for (int i = 0; i < sizeof(data); i++) { | |
cout << bitset<8>(dataBuffer[i]) << endl; | |
} | |
return 0; | |
} | |
/* | |
Output result should be | |
11111111 | |
11111111 | |
11111111 | |
11111111 | |
11111111 | |
11111111 | |
11111111 | |
11111111 | |
11111111 | |
11111111 | |
11111110 | |
00010101 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment