c++ - Storing 8 logical true/false values inside 1 byte? -
i'm working on microcontroller 2kb of sram , desperately need conserve memory. trying work out how can put 8 0/1 values single byte using bitfield can't quite work out.
struct bits { int8_t b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; }; int main(){ bits b; b.b0 = 0; b.b1 = 1; cout << (int)b.b0; // outputs 0, correct cout << (int)b.b1; // outputs -1, should outputting 1 } what gives?
all of bitfield members signed 1-bit integers. on two's complement system, means can represent either 0 or -1. use uint8_t if want 0 , 1:
struct bits { uint8_t b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; };
Comments
Post a Comment