Wednesday, May 13, 2015

Bit Change



#include<stdio.h>
void showbits(unsigned char n);
int main()
{
        unsigned char num=170,num1=167,p,r,s,t,z;
        int w,q;
        printf("\nvalue of num=");
        showbits(num);
        printf("\nvalue of num1=");
        showbits(num1);
        printf("\nHow many bits you want to strip (1-7)=");
        scanf("%d",&w);
        p=w;
        printf("\nwhere do you want to change (1-7)=");
        scanf("%d",&q);
        if(p==1)
        {
                z=num1 & 0x1;
                showbits(z);
        }
        else if(p==2)
        {
                z=num1 & 0x3;
                showbits(z);
        }
        else if(p==3)
        {
                z=num1 & 0x7;
                showbits(num1);
        }
        else if(p==4)
        {
                z=num1 & 0xF;
                showbits(z);
        }
        else if(p==5)
        {
                z=num1 & 0x1F;
showbits(z);
  }
        else if(p==6)
        {
                z=num1 & 0x3F;
                showbits(z);
        }
        else if(p==7)
        {
                z=num1 & 0x7F;
                showbits(z);
        }
        else
        {
                z=num1&0XFF;
                showbits(z);
        }
        r=z<<(q-p);
        printf("\nMoved bit=");
        showbits(r);
        s=num|r;
        printf("\nvalue of new num=");
        showbits(s);
}
void showbits(unsigned char n)
{
        int i;
        unsigned char j,k,andmask;
        for(i=7;i>=0;i--)
        {
                j=i;
                andmask = 1<<j;
                k=n&andmask;
                if(k==0)
                        printf("0");
                else
                        printf("1");
        }
}

No comments:

Post a Comment