初学者c语言程序,位域,二进制。

img


要有解题过程,推断过程,初学者的苦恼。位域,二二进制。推理过程。

给你画了一个图理解哈!答案应该是B

img


#include <stdio.h>
#pragma pack(push,1)
union U {
    unsigned char byte;
    struct BF {
        unsigned int b0:1;//a
        unsigned int b1:1;//b
        unsigned int b2:1;//c
    } bf;
} u;
struct DATE {
    unsigned int DayOfMonth  : 5;//bit04~bit00
    unsigned int Month       : 4;//bit08~bit05
    unsigned int Year        :12;//bit20~bit09
    unsigned int Spares      :11;//bit31~bit21
} *pD;
struct TIME {
    unsigned int Milliseconds:10;//bit09~bit00
    unsigned int Seconds     : 6;//bit15~bit10
    unsigned int Minutes     : 6;//bit21~bit16
    unsigned int Hours       : 5;//bit26~bit22
    unsigned int Spares      : 5;//bit31~bit27
} *pT;
#pragma pack(pop)
unsigned char bt;
int a,b,c;
unsigned char s[8]={0x00,0x0F,0xAE,0xF9,0x04,0x6D,0x00,0x00};
// bbbbbbbb bbbbbbbb bbbbbbbb bbbbbbbb
// iiiiiiii iiiiiiii iiiiiiii iiiiiiii
// tttttttt tttttttt tttttttt tttttttt
// 33222222 22221111 11111100 00000000
// 10987654 32109876 54321098 76543210
// :::::::: :::::::: :::::::: ::::::::
//
//             YYYYY YYYYYYYM MMMDDDDD
// 00000000 00001111 10101110 11111001  0x00,0x0F,0xAE,0xF9
//                      2007    7   25
//
//      hhh hhmmmmmm ssssssii iiiiiiii
// 00000100 01101011 00000000 00000000  0x04,0x6D,0x00,0x00
//          17    45     00        000

unsigned char d[8];
int main() {
    for (bt=0;bt<8;bt++) {
        u.byte=(unsigned char)bt;
        a=u.bf.b0;
        b=u.bf.b1;
        c=u.bf.b2;
        printf("byte 0x%02x -- c:%d b:%d a:%d\n",bt,c,b,a);
    }
    for (c=0;c<2;c++)
    for (b=0;b<2;b++)
    for (a=0;a<2;a++) {
        u.bf.b0=a;
        u.bf.b1=b;
        u.bf.b2=c;
        bt=u.byte;
        printf("c:%d b:%d a:%d -- byte 0x%02x\n",c,b,a,bt);
    }

    for (int i=0;i<4;i++) {
        d[  i]=s[  4-1-i];
        d[4+i]=s[4+4-1-i];
    }

    pD=(struct DATE *)&d[0];
    printf("%04u-%02u-%02u\n",
        pD->Year        ,
        pD->Month       ,
        pD->DayOfMonth  );

    pT=(struct TIME *)&d[4];
    printf("%02u:%02u:%02u.%03u\n",
        pT->Hours       ,
        pT->Minutes     ,
        pT->Seconds     ,
        pT->Milliseconds);
    return 0;
}
//byte 0x00 -- c:0 b:0 a:0
//byte 0x01 -- c:0 b:0 a:1
//byte 0x02 -- c:0 b:1 a:0
//byte 0x03 -- c:0 b:1 a:1
//byte 0x04 -- c:1 b:0 a:0
//byte 0x05 -- c:1 b:0 a:1
//byte 0x06 -- c:1 b:1 a:0
//byte 0x07 -- c:1 b:1 a:1
//c:0 b:0 a:0 -- byte 0x00
//c:0 b:0 a:1 -- byte 0x01
//c:0 b:1 a:0 -- byte 0x02
//c:0 b:1 a:1 -- byte 0x03
//c:1 b:0 a:0 -- byte 0x04
//c:1 b:0 a:1 -- byte 0x05
//c:1 b:1 a:0 -- byte 0x06
//c:1 b:1 a:1 -- byte 0x07
//2007-07-25
//17:45:00.000
//