Friday 15 June 2012

c - Store zeros from ints and use them later -



c - Store zeros from ints and use them later -

i have 3 sensors each provide either 0 or 1 (repeatedly in loop). stored individually int variables. these printed using following:

print ("%d%d%d", sensor1, sensor2, sensor3);

i want store each combination (ex: 010, 001, 110, etc.) temporarily can utilize else (i want have switch or can different operation depending on value of sensor combination). can't store int since drops 0s in front.

how can store these combinations?

you can utilize construction bit field this.

struct bit{ bool sensor1 : 1; bool sensor2 : 1; bool sensor3 : 1; }; int main(void) { struct bit bit = {0, 1, 0}; printf ("%d%d%d", bit.sensor1, bit.sensor2, bit.sensor3); }

c

No comments:

Post a Comment