C Program for Bitwise Operations.

#include "stdio.h"

int main()
{
int n,i=0;
scanf("%d",&n);
while(n!=0)
{
if((1&n)!=0)
i++;
n>>1;
}
printf("%d",i);
}

4 comments:

  1. no output is coming

    ReplyDelete
    Replies
    1. makes only small change in second last statement is n >>= 1 ;it gives an output

      Delete
  2. program to implement bitwise operators

    #include
    #include
    int disp(int x);
    void main()
    {
    int i,j,m,n,w,a,b=12;
    clrscr();
    do
    {
    printf("enter any two numbers\n");
    scanf("%d%d",&i,&j);
    printf("1:one's complement\n2:left shift\n3:right shift\n4:AND\n5OR\n6:XOR\n7:end");
    printf("\nenter ur choice");
    scanf("%d",&n);
    switch(n)
    {
    case 1:
    {
    printf("1's complement operation\n");
    m=~i;
    n=~j;
    printf("1's complement of %d is :\t",i);
    disp(m);
    printf("1's complement of %d is :\t",j);
    disp(n);
    break;
    }
    case 2:
    {
    printf("right shift operation");
    for(a=0;a<=3;a++)
    {
    m=i>>a;
    printf("%d right shift of %d gives\n",i,a);
    disp(m);
    n=j>>a;
    printf("\n%dright shift gives",j,a);
    disp(n);
    }
    break;
    }
    case 3:
    {
    printf("left shift operation\n ");
    for(a=0;a<=3;a++)
    {
    m=i<=0;p--)
    {
    s=i<<p;
    q=x<<s;
    (q==0?printf("0")|printf("1"));
    }
    }

    ReplyDelete