Mam taki problem że chcę wyświetlić wartości 8 bitów z jednego bajta ale nie bardzo wiem jak mogę to zrobić.
Jeżeli mógł by mi wytłumaczyć jak to można zrobić będę wdzięczny. :)
0
3
typedef struct Bits{
char bits[CHAR_BIT];
} Bits;
Bits bits(unsigned char c)
{
Bits b;
for(int i = CHAR_BIT-1; i >= 0; --i){
b.bits[i] = (c & 1) + '0';
c >>= 1;
}
return b;
}
0
http://stackoverflow.com/questions/3497345/is-there-a-way-to-access-individual-bits-with-a-union
Można to też osiągnąć (moim zdaniem łatwiej) za pomocą pól bitowych w unii
1
można to potraktować jak maszynke do robienia frytek z zieminiaka ;d. Wkładasz (ziemniaka) do maszyny (struktura) i wyciągasz sobie bity (frytki)
typedef union
{
struct
{
unsigned char bit1 : 1;
unsigned char bit2 : 1;
unsigned char bit3 : 1;
unsigned char bit4 : 1;
unsigned char bit5 : 1;
unsigned char bit6 : 1;
unsigned char bit7 : 1;
unsigned char bit8 : 1;
}u;
unsigned char status;
}DeviceStatus;
unsigned char c = 4;
DeviceStatus ds;
ds = c;
ds.u.bitn