Pages

Tuesday 4 September 2012

Bit Magic


1.Given a integer x, write a function that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, /, *.

Examples:
Input: 2
Output: 7

Input: 5
Output: 17 (Ignore the digits after decimal point)

#include<stdio.h>
int main()
{
    int i,j,k;
    printf("enter number");
    scanf("%d",&i);
    j=i<<1;
    k=i>>1;
    i=i+j+k;
    printf("\n number after multiplying with 3.5 is %d ",i);
    return 0;
}

2.Count total set bits in all numbers from 1 to n

#include<stdio.h>
int main()
{
    int i=0,n;
    scanf("%d",&n);
    while(n>0)
    {
    i++;
    n=n&(n-1);   
    }
    printf("%d",i);
}

3.Smallest of three integers without comparison operators

#include<stdio.h>
int main()
{
    int i,j,k;
    scanf("%d%d%d",&i,&j,&k);
    while(i&&j&&k)
    {
    !(--i)&&printf("first number is small")||!(--j)&&printf("second number is small")||!(--k)&&printf("3rd number is small");   
    }
}

4.Program to count number of set bits in an (big) array

#include<stdio.h>
int fun(int n)
{
    int i=0;
    while(n>0)
    {
    i++;
    n=n&(n-1);   
    }
    return i;
}
int main()
{
    int i,k,j,sum=0;
    printf("enter no of elements in array\n");
    scanf("%d",&i);
    for(j=0;j<i;j++)
    {
    scanf("%d",&k);   
    sum=sum+fun(k);
    }
    printf("\ntotal number of set bits is %d",sum);
}

5.Count number of bits to be flipped to convert A to B
int fun(int n)
{
    int i=0;
    while(n>0)
    {
    i++;
    n=n&(n-1);   
    }
    return i;
}
#include<stdio.h>
int main()
{
    int a,b,c;
    printf("enter  a and b values");
    scanf("%d %d",&a,&b);
    c=a^b;
    printf("number of bits to filp to convert a to b is %d",fun(c));
}


6.Swap bits in a given number

Given a number x and two positions (from right side) in binary representation of x, write a function that swaps n bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap.

Examples:

Let p1 and p2 be the two given positions.

Input:
x = 47 (00101111)
p1 = 1 (Start from second bit from right side)
p2 = 5 (Start from 6th bit from right side)
n = 3 (No of bits to be swapped)
Output:
227 (11100011)
The 3 bits starting from the second bit (from right side) are
swapped with 3 bits starting from 6th position (from right side) 

#include<stdio.h>
int main()
{
    int n,i,j,k,m;
    printf("enter number");
    scanf("%d",&n);
    printf("enter positions");
    scanf("%d %d",&i,&j);
    m=j;
    for(k=i;k<m&&j<32;)
    {
     if(n&(1<<k)&&n&(1<<j));//when both digits 1 no need to swap
     else if(!(n&(1<<k))&&!(n&(1<<j)));//when both digits no need to swap
     else //when both digits are not same do swap
     {
        n=n^(1<<k);
        n=n^(1<<j);       
      }
     k++;
    j++;
    }
    printf("%d",n);
    return 0;
}

7.Add 1 to a given number

Write a program to add one to a given number. You are not allowed to use operators like ‘+’, ‘-’, ‘*’, ‘/’, ‘++’, ‘–’ …etc.

Examples:
Input: 12
Output: 13

Input: 6
Output: 7

#include<stdio.h>
int main()
{
    int i,k=0;
    printf("\n enter numeber ");
    scanf("%d",&i);
    while(i&(1<<k))//go untill u find 0 digit in number then change it as 1
    {
     i=i^1<<k;   
     k++;
    }
    i=i^1<<k;
    printf("%d",i);
}

   If im wrong please correct me or give comment and you can post your solutions and questions here