Tuesday 19 April 2022

Bit wise operators Questions in C

Bitwise And

  1. #include<stdio.h>
  2. int main()
  3. {
  4. int n;
  5. printf("Enter an integer\n");
  6. scanf("%d",&n);
  7. if ( n & 1 == 1 )
  8. printf("Odd\n");
  9. else
  10. printf("Even\n");
  11. return 0;
  12. }

Bitwise XOR 

Symbol used is ^

#include <stdio.h>
main()
{
 int x=1,y=3;
 if(x ^ y)
  printf("x is not equal to y");
 else
  printf("x is equal to y");
}

Practice Problem

https://www.codechef.com/problems/MISSP

Chef is fan of pairs and he likes all things that come in pairs. He even has a doll collection in which all dolls have paired.One day while going through his collection he found that there are odd number of dolls. Someone had stolen a doll!!!
Help chef find which type of doll is missing..

#include <stdio.h>
   
    int main()
{
        int t, n, m, b;
       
           
        scanf ("%d\n", &t);
        while (t--)
{
            b = 0;
           
            scanf("%d\n", &n);
           
            while (n--)
{
                scanf("%d\n", &m);
                b ^= m;
            }
           
            printf("%d\n", b);
        }
       
        return 0;
    }

Bitwise <<


No comments:

Post a Comment