| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1289 人关注过本帖, 1 人收藏
标题:C语言位字段问题
只看楼主 加入收藏
C2month
Rank: 1
等 级:新手上路
帖 子:4
专家分:7
注 册:2018-5-1
收藏(1)
 问题点数:0 回复次数:0 
C语言位字段问题
下面程序中红色标出的box.us_view是怎么被初始化的,没看懂。 Union变量被初始化之后, 难道不是只储存了位字段吗?box.us_view没有初始化,怎么会有值,
程序代码:
#include<stdio.h>
#include<limits.h>
#include<stdbool.h>
//位字段符号常量
//边框线样式
#define SOLID 0
#define DOTTED 1
#define DASHED 2
//三原色
#define BLUE 4 //100
#define GREEN 2 //010
#define RED 1 //001
//混合色
#define BLACK 0
#define YELLOW (RED|GREEN) //011, 3
#define MAGENTA (RED|BLUE) //101, 5
#define CYAN (GREEN|BLUE) //110, 6
#define WHITE (RED|GREEN|BLUE) //111, 7
//按位方法中用到的符号常量
#define OPAQUE       0x1
#define FILL_BLUE    0x8
#define FILL_GREEN   0x4
#define FILL_RED     0x2
#define FILL_MASK    0xE
#define BORDER       0x100
#define BORDER_BLUE  0x800
#define BORDER_GREEN 0x400
#define BORDER_RED   0x200
#define BORDER_MASK  0xE00
#define B_SOLID      0
#define B_DOTTED     0x1000
#define B_DASHED     0x2000
#define STYLE_MASK   0x3000

const char *colors[8] = { "black","red","green","yellow","blue","magenta",
"cyan","white" };

struct box_props{
    bool opaque               : 1;
    unsigned int fill_color   : 3;
    unsigned int              : 4;
    bool show_border          : 1;
    unsigned int border_color : 3;
    unsigned int border_style : 2;
    unsigned int              : 2;
};

union Views {
    struct box_props st_view;
    unsigned short us_view;
};
void show_settings(const struct box_props *pb);
void show_setting1(unsigned short);
char *itobs(int n, char *ps);

int main(void)
{
    //创建Views联合,并初始化struct box view
    union Views box = { {true,YELLOW,true,GREEN,DASHED} };
    char bin_str[8 * sizeof(unsigned int) + 1];

    printf("Original box settings:\n");
    show_settings(&box.st_view);
    printf("\nBox settings using unsigned int view:\n");
    show_setting1(box.us_view);

    printf("bits are %s\n",
        itobs(box.us_view, bin_str));
    box.us_view &= ~FILL_MASK; //把表示填充色的位清0
    box.us_view |= (FILL_BLUE | FILL_GREEN); //重置填充色
    box.us_view ^= OPAQUE; //切换是否透明的位
    box.us_view |= BORDER_RED;//错误的方法
    box.us_view &= STYLE_MASK;
    box.us_view |= B_DOTTED;//把样式设置为点
    printf("\nModified box setting:\n");
    show_settings(&box.st_view);
    printf("\nBox settings using unsigned int view:\n");
    show_setting1(box.us_view);

    printf("bits are %s\n",
        itobs(box.us_view, bin_str));

    system("pause");
    return 0;
}
void show_settings(const struct box_props *pb)
{
    printf("Box is %s.\n", pb->opaque == true ? "opaque" : "transparent");
    printf("The fill color is %s.\n", colors[pb->fill_color]);
    printf("Border is %s\n",
        pb->show_border == true ? "shown" : "not shown");
    printf("The border color is %s.\n", colors[pb->border_color]);
    printf("The border style is ");
    switch (pb->border_style)
    {
    case SOLID :printf("solid.\n"); break;
    case DOTTED:printf("dotted.\n"); break;
    case DASHED: printf("dashed.\n"); break;
    default    :printf("unknown type.\n"); 

    }
}

void show_setting1(unsigned short us)
{
    printf("Box is %s.\n", (us&OPAQUE) == true ? "opaque" : "transparent");
    printf("The fill color is %s.\n", colors[(us>>1)&07]);
    printf("Border is %s",
        (us&BORDER) == true ? "shown" : "not shown");
    printf("The border color is %s.\n", colors[(us >> 9) & 07]);
    printf("The border style is ");
    switch (us&STYLE_MASK)
    {
    case SOLID:printf("solid.\n"); break;
    case DOTTED:printf("dotted.\n"); break;
    case DASHED: printf("dashed.\n"); break;
    default:printf("unknown type.\n");

    }
}
char *itobs(int n, char *ps)
{
    int i;
    const static int size = CHAR_BIT * sizeof(int);

    for (i = size - 1; i >= 0; i--, n >> 1)
        ps[i] = (01 & n) + '0';
    ps[size] = '\0';

    return ps;
}







搜索更多相关主题的帖子: box define unsigned int printf 
2018-06-09 22:23
快速回复:C语言位字段问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017050 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved