Showing posts with label BIT WISE Operators -> C Interview Questions. Show all posts
Showing posts with label BIT WISE Operators -> C Interview Questions. Show all posts

Friday, 5 July 2013

BIT WISE Operators -> C Interview Questions

Hi All,

  In this post, i would like to show frequently asked C-FAQs that are being encountered in interviews.

BIT-WISE Operators C-Faqs:

1. How to set particular bit in a number?
To set any bit in a number, Use (OR ) operator.

Number = Number | (1<<x)
'x' indicates the position of the bit
 

2. How to clear particular bit in a number?
To clear any bit in a number, Use (AND ) and (NEG (~)) operator.

Number = Number & ~(1<<x)
 
3. How to toggle or flip particular bit in a number?
 To toggle any bit in a number, Use (^ ) exclusive OR operator. 


Number = Number ^ (1<<x)

4.How to check particular bit is set or not in a number?
To check any bit in a number, Use (& ) And Operator . 

Number & (1<<x) 
                                                      'x' indicates the position of the bit  

5. How to represent the above in MACRO's for Real time code?

#define SET_BIT(Number, bit_position)    Number |= (1<< bit_position)

#define CLEAR_BIT(Number, bit_position)    Number &= ~(1<< bit_position)

#define TOGGLE_BIT(Number, bit_position)    Number ^= (1<< bit_position) 

#define CHECK_BIT_IS_SET_OR_NOT(Number, bit_position)    Number & (1<< bit_position)

6. How to check the number is Even or Odd?

#include<stdio.h>
int main()
{
  int number;
  printf("Enter the Number \n");
  scanf("%d", &number);

  if(number & 1)
    printf(" It is ODD number \n");
  else
    printf(" It is Even number \n");
}
 

6. How to count number of  1's or bits set in a number ?

#include<stdio.h>
int main()
{
  int n, count=0;
  printf(" Enter the Number :\n");
  scanf(" %d", &n);

  while(n)
  {
    n = n&(n-1);
    count ++;
  }
  printf(" No of Bits set count in the Number is %d\n", count);
  return 0;
}

 

7. How to count number of  0's or bits unset in a number ?
 
#include<stdio.h>
#define CHAR_BYTE 8
int main()
{
  unsigned int num, count =0, index;
  printf(" Enter the Integer \n");
  scanf("%d", &num);

  for(index = 0; index < (CHAR_BYTE * sizeof(int)); index++)
  {
     /* check the Bit is Set to Zero */
     if((num & (1 << index)) == 0)
     {
        count ++;
     }

  }
  printf(" The Number of Zero's present in Integer: %d\n", count);
  return 0;
}

 8. How to find out a number  is power of 2  or not? 

 #include<stdio.h>
int main()
{
  unsigned int n;
  printf("Enter the Number \n");
  scanf("%d", &n);

  if((n != 0) && (n & (n-1)) )
      printf(" It is Not power of 2\n");
  else
      printf(" It is power of 2\n");

 return 0;
}



 9. How to Swap two numbers using Bit Wise Operators?  ( Remember as -> (ab, ba, ab)

#include<stdio.h>
int main()
{
   int a, b;
   printf(" Enter the Value of a and b\n");
   scanf("%d %d", &a, &b);
   a = a ^ b;
   b = b ^ a;
   a = a ^ b;
   


/*
   or 
   a = a + b;
   b = a - b;
   a = a - b;
   */
 

   printf("Value of a : %d and b : %d\n", a, b);  
   return 0;
}

10. How to reverse a string with out using Temporary variable?

#include<stdio.h>
#include<string.h>

#define LEN 100 /* Can be changed */
int main()
{
   char str[LEN];   
   unsigned int end, start=0;

   printf(" Enter the String to be reversed  \n");
   scanf("%s", str);

   /* Find out the string Length */
   end = strlen(str)-1;
   /* Iterate through start less than end */
   while(start < end)  
   {
     str[start]   ^= str[end];
     str[end]     ^= str[start];
     str[start]   ^= str[end];
    
     start++;
     end --;
   }

   printf("The Reverse string is : %s\n", str);
   return 0;
}



10. How to reverse Bits of a Number?

#include<stdio.h>
#define SIZE_OF_CHAR 8
int main()
{

    unsigned int i, num, reverse_num= 0;
    unsigned int NO_OF_BITS;

    printf("Enter the number to be reversed \n");
    scanf("%d", &num);

    NO_OF_BITS = SIZE_OF_CHAR * (sizeof(num));

   for(i=0; i<NO_OF_BITS; i++)
   {
       if(num & (1<<i))          -> This similar Logic is used to count 1's & 0's in a number.
       {
           reverse_num = reverse_num | (1<< ((NO_OF_BITS -1)-i));
       }
   }

   printf(" The reverse Num is %d", reverse_num);

}

Please refer the BITMAP tutorial in the BLOG, It is really useful for real time applications.