Powered By Blogger

Saturday 30 April 2011

Can u guess the output... U jst need a simple concept of bitwise and logical operators in C

#include<stdio.h>
int main(void)
{
    int a[5],i=0,s=0;
    printf("Enter 5 digits (choose digits between 0-9)::\n");
    for(i=0;i<5;i++)
        scanf("%d",&a[i]);


    printf("\n\n");
    for(i=0;i<4;i++)
        printf("%5d|%d=%d",a[i+1],a[i],a[i+1]|a[i]);



    printf("\n\n");
    for(i=0;i<4;i++)
        printf("%5d||%d=%d",a[i+1],a[i],a[i+1]||a[i]);
  
  

    printf("\n\n");
    for(i=0;i<4;i++)
        printf("%5d&%d=%d",a[i+1],a[i],a[i+1]&a[i]);


    printf("\n\n");
    for(i=0;i<4;i++)
        printf("%5d&&%d=%d",a[i+1],a[i],a[i+1]&&a[i]);

    getch();
}


EXPLANATION:
For bit wise operator the result is produced as expected..Those bit wise operations are performed on the binary equivalent of those two number and then final output is printed as decimal no.
exmp: 2||4=6 as 010||100=110

In case o logical operators.. U can think like, all the values  except '0' is considered as '1' and then output is generated after performing "AND" or "OR" operation on those numbers.
exmp: 0&&2=0.

No comments:

Post a Comment