AG Anvesh G 29 Nov 2026 1 views 0 comments C Programming

Bitwise Exercise

/*E.1*/
#include<stdio.h>
int main(void)
{
    int x=7,y=19;
    printf("%d  %d  ",x&y,x&&y);
    printf("%d  %d  ",x|y,x||y);
    printf("%d\n",x^y);
    return 0;
}

/*E.2*/
#include<stdio.h>
void displayBits(int x);
int main(void)
{
    unsigned int x,y,z;
    displayBits(0xFFFF);
    x=y=z=0xFFFF;
    x=(x>>5)<<5;    displayBits(x);
    y=(y>>3)<<3;    displayBits(y);
    z=(z>>2)<<2;    displayBits(z);
    return 0;
}
void displayBits(int x)
{
    int i,mask;
    for(i=31; i>=0; i--)
    {
        mask = 1<<i;
        putchar((x & mask)?'1':'0'); /*Test and print ith bit*/    
        if(i%8==0)
            putchar(' '); /*Space after 8 bits*/
    }
    printf("\n");
}

/*E.3*/
#include<stdio.h>
int main(void)
{
    int k;
    k=((3<<4)^(96>>1));
    printf("%d\n",k);
    return 0;
}

/*E.4*/
#include<stdio.h>
int main(void)
{
    int x=0x1F;
    x<<2;
    printf("%X  ",x);
    x>>2;
    printf("%X\n",x);
    return 0;
}

/*E.5*/
#include<stdio.h>
int main(void)
{
    unsigned int arr_mask[]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200,
    0x400, 0x800, 0x1000,0x2000, 0x4000, 0x8000, 0x10000, 0x20000, 0x40000, 0x80000,  
    0x100000, 0x200000,0x400000, 0x800000, 0x1000000, 0x2000000, 0x4000000, 0x8000000,
    0x10000000, 0x20000000, 0x40000000, 0x80000000};

    int i,x=0x54038F;
    for(i=31; i>=0; i--)
        x&arr_mask[i] ? putchar('1'): putchar('0');
    return 0;
}

/*E.6*/
#include<stdio.h>
int main(void)
{
    unsigned int num=0xA01D,pos=3,bit;
    unsigned int mask=1<<pos;
    bit=(num&mask)>>pos;
    printf("%u\n", bit);
    return 0;
}

/*E.7*/
#include<stdio.h>
int main(void)
{
    unsigned int num=0x1F,pos=3,bit;
    bit=(num>>pos)&1;
    printf("%u\n",bit);
    return 0;
}

/*E.8*/
#include<stdio.h>
int main(void)
{
    int i,num=0xA0DF;
    for(i=31; i>=0; i--)
        printf("%d",(num>>i)&1);
    return 0;
}

/*E.9*/
#include<stdio.h>
int main(void)
{
    int i,num=0x1A3B;
    unsigned int mask=1<<31;  
    for(i=31; i>=0; i--)
    {
        (num & mask) ? printf("1") : printf("0");
        mask=mask>>1;
    }
    return 0;
}
     
/*E.10*/
#include<stdio.h>
void func(int x);
int main(void)
{
    func(0x1AE3);
}
void func(int x)
{
    int i,mask;
    mask=1<<31;
    for(i=1; i<=32; i++)
    {
        putchar((x&mask)?'1':'0');
        x<<=1;
        if(i%8==0)
            putchar(' ');
    }
    printf("\n");
    return 0;
}


Share:

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

Bit wise operators Assignment

Bitwise Operators practical assignment: Take runtime input, bit-level manipulation, and different data units r...

AG Anvesh G 27 Sep 2026
Understanding Float and Double Binary Representation in C

Understanding Float and Double Binary Representation in CIntroduction:When we store decimal numbers such as 10...

AG Anvesh G 31 May 2026
Topic-wise C Programming Interview Questions

C Programming Interview Questions – Topic Wise1. IntroductionC programming is one of the most important langu...

AG Anvesh G 30 May 2026