Systemnahe Programmierung in C: Bitfelder |
1#include "dumpmem.h"
2
3typedef struct
4{
5 unsigned int day:5;
6 unsigned int month:4;
7 unsigned int year:11;
8}
9Date;
10
11Date d0;
12
13int
14main (void)
15{
16 Date d;
17
18 d = d0;
19 d.day = 0x1f;
20 dumpMem (&d, sizeof d);
21
22 d = d0;
23 d.month = 0x0f;
24 dumpMem (&d, sizeof d);
25
26 d = d0;
27 d.year = 0x7ff;
28 dumpMem (&d, sizeof d);
29
30 d = d0;
31 d.day = 0x1f;
32 d.month = 0x0f;
33 d.year = 0x7ff;
34 dumpMem (&d, sizeof d);
35
36 return 0;
37}
|
1#include "dumpmem.h"
2
3#include <stdio.h>
4#include <stdlib.h>
5
6void
7dumpMem (void *start, size_t len)
8{
9 unsigned char *p;
10 size_t i;
11
12
13 printf ("\n");
14 printf (" 7 0\n");
15 printf (" ---------------------------------\n");
16
17 for (i = 0, p = start; i < len; ++i, ++p)
18 {
19 int j;
20
21 printf ("%4u : |", i);
22 for (j = 7; j >= 0; --j)
23 {
24 printf (" <blue>%1u</blue> |", (*p >> j) & 1);
25 }
26 printf ("\n");
27 printf (" ---------------------------------\n");
28
29 }
30
31 printf ("\n");
32}
|
Letzte Änderung: 11.01.2007 | © Prof. Dr. Uwe Schmidt |