第15章 位操作

使用位运算现实二进制

#include

char * itobs (int n , char *ps );

void show_bstr(const char *);

int invert_end ( int num,int bits);

int main()

{

    char bin_str[8 * sizeof(int) + 1];

    int number ;

    printf("enter integers and see them in binary ");

    puts("non - numeric input terminates program");

    while (scanf("%d",&number) == 1) {

        itobs(number, bin_str);

        printf("%d is \n",number);

        show_bstr(bin_str);

        putchar('\n');

//        number = invert_end(number , 4);

//        printf("inverting the last b bits gives \n");

//        show_bstr(itobs(number, bin_str));

    }

}

char  *itobs (int n ,char *ps)

{

    int i;

    static int size = 8 * sizeof(int);

    for (i = size -1 ; i >= 0 ; i -- ,n >>= 1) {

        ps[i] = (01 & n ) + '0' ;

    }

    ps[size]='\0';

    return ps;

}

void show_bstr(const char *str)

{

    int i = 0;

    while (str[i]) {

        putchar(str[i]);

        if (++ i % 4 == 0 && str[i]) {

            putchar(' ');


        }

    }

}





使用位操作符来显示二进制


#include

char * itobs (int n , char *ps );

void show_bstr(const char *);

int invert_end ( int num,int bits);

int main()

{

    char bin_str[8 * sizeof(int) + 1];

    int number ;

    printf("enter integers and see them in binary ");

    puts("non - numeric input terminates program");

    while (scanf("%d",&number) == 1) {

        itobs(number, bin_str);

        printf("%d is \n",number);

        show_bstr(bin_str);

        putchar('\n');

        number =invert_end(number , 4);

        printf("inverting the last b bits gives \n");

        show_bstr(itobs(number, bin_str));

        putchar('\n');


    }

    puts("Bye");


}

char  *itobs (int n ,char *ps)

{

    int i;

    static int size = 8 * sizeof(int);

    for (i = size -1 ; i >= 0 ; i -- ,n >>= 1) {

        ps[i] = (01 & n ) + '0' ;

    }

    ps[size]='\0';

    return ps;

}

void show_bstr(const char *str)

{

    int i = 0;

    while (str[i]) {

        putchar(str[i]);

        if (++ i % 4 == 0 && str[i]) {

            putchar(' ');


        }

    }

}

int invert_end(int num,int bits)

{

    int mask = 0;

    int bitval = 1;

    while(bits--  >0) {

        mask |= bitval;

        bitval <<=1;


    }

    return num^mask ;


}






定义和使用字段

#include

#define YES 1

#define NO 0

#define SOLID 0

#define DOTTED 1

#define DASHED 2

#define BLUE 4

#define GREEN 2

#define RED 1

#define BLACK 0

#define YELLOW (RED | GREEN)

#define MAGENTA (RED | BLUE)

#define CYAN (GREEN | BLUE)

#define WHITE (RED | GREEN | BLUE)

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

struct box_props{

    unsigned intopaque      :1;

    unsigned intfill_color  :3;

    unsigned int              :4;

    unsigned intshow_border  :1;

    unsigned int border_color :3;

    unsigned int border_style :1;

    unsigned int              :1;



};

void show_settings (const struct box_props *pb);

int main(void)

{

    struct box_props box= {YES , YELLOW ,YES,GREEN,DASHED};

    printf("Orginal box settings : \n");

    show_settings(&box);






}

void show_settings (const struct box_props *pb)

{

    printf("box is %s \n",pb->opaque ==YES ? "opaue": "transparent");

    printf("the fill color is %s .\n",colors [pb->fill_color]);

    printf("Boreder %s \n",pb->show_border == YES? "shown":"notshwn");

    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");


    }

}




位字段运算

#include

#define YES 1

#define NO 0

#define SOLID 0

#define DOTTED 1

#define DASHED 2

#define BLUE 4

#define GREEN 2

#define RED 1

#define BLACK 0

#define YELLOW (RED | GREEN)

#define MAGENTA (RED | BLUE)

#define CYAN (GREEN | BLUE)

#define WHITE (RED | GREEN | BLUE)

#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","while"};

struct box_props{

    unsigned intopaque      :1;

    unsigned intfill_color  :3;

    unsigned int              :4;

    unsigned intshow_border  :1;

    unsigned int border_color :3;

    unsigned int border_style :2;

    unsigned int              :2;

};

union Views

{

    struct box_props st_view;

    unsigned int ui_view;


};

void show_settings (const struct box_props *pb);

void show_settings1(unsigned short);

char * itobs(int n,char * ps);

int main(void)

{

    union Views box = {{ YES, YELLOW,YES,GREEN,DASHED}};

    char bin_str[8 * sizeof(unsigned int) +1];

    printf("Original box settings \n");

    show_settings(&box.st_view);

    printf("\n Box setting using unsigned int view \n");

    show_settings1(box.ui_view);

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

    box.ui_view &= ~FILL_MASK;

    box.ui_view |= (FILL_BLUE | FILL_GREEN);

    box.ui_view ^= OPAQUE;

    box.ui_view |= BORDER_RED;

    box.ui_view &= ~STYLE_MASK;

    box.ui_view |= B_DOTTED;

    printf("\n modified box sttings\n");

    show_settings(&box.st_view);

    printf("\n Box settings using unsigned\n");

    show_settings1(box.ui_view);

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


    return 0;


}

char *itobs(int n , char* ps)

{

    int i ;

    static int size = 8 * sizeof(unsigned int );

    for (i = size -1 ; i >= 0; i -- ,n>>= 1) {

        ps[i] = (01 & n ) + '0';

    }

    ps[size] ='\0';

    return ps;

}

void show_settings (const struct box_props *pb)

{

    printf("box is %s \n",pb->opaque ==YES ? "opaue": "transparent");

    printf("the fill color is %s .\n",colors [pb->fill_color]);

    printf("Boreder %s \n",pb->show_border == YES? "shown":"notshwn");

    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_settings1(unsigned short us)

{

    printf("box is %s \n",us& OPAQUE == OPAQUE? "opaque" : "transparent");

    printf("the fill color  is %s \n", colors[(us>>1) &7]);

    printf("Border %s \n",us & BORDER == BORDER? "shown" : "not shown");

    printf("the border style is");

    switch (us & STYLE_MASK ) {

        case B_SOLID:

            printf("solid.\n");

            break;

        case B_DOTTED:

            printf("dotted \n ");

            break;

        case B_DASHED:

            printf("dashed \n ");

            break;



        default:

             printf("unknown type \n");

    }


    printf("the border color is %s \n",colors[(us>>9)& 07] );




}




#include

#define TWO 2

#define OW "Consistency is the last refuge of the unimagin\ tive. -Oscar Wilde"

#define FOUR TWO * TWO 

#define PX printf("X is %d.\n",x)

#define FMT "X is %d.\n"

int main(void )

{

    int x = TWO;

    PX;

    x =FOUR;

    printf(FMT ,x);

    printf("%s \n", OW);

    printf("TWO : OW\n");

    return 0;

}



你可能感兴趣的:(第15章 位操作)