C - Operator Using bitwise &

Introduction

AND operation masks bit values.

The bitwise & masks out the sixth bit, causing its value to be reset to 0 in the final calculation.

Demo

#include <stdio.h>
#define SET 223/*from   w ww  .j  a  v  a 2s .c  o  m*/

char *to_binary(int n);

int main()
{
    int bor,result;

    printf("Type a value from 0 to 255: ");
    scanf("%d",&bor);
    result = bor & SET;

    printf("\t%s\t%d\n",to_binary(bor),bor);
    printf("&\t%s\t%d\n",to_binary(SET),SET);
    printf("=\t%s\t%d\n",to_binary(result),result);
    return(0);
}

char *to_binary(int n)
{
    static char bin[9];
    int x;

    for(x=0;x<8;x++)
    {
        bin[x] = n & 0x80 ? '1' : '0';
        n <<= 1;
    }
    bin[x] = '\0';
    return(bin);
}

Result

Related Topic