Play with Bits

Find the total number of set bit in given binary number ?
 

#include <stdio.h>
int countSetBits(unsigned int n)
{
        unsigned int count = 0;
        while(n)
        {
                count = count +  (n & 1);
                n >>= 1;
        }
        return count;
}
/* Program to test function countSetBits */
int main()
{
        int i;
        printf("Enter The Number : ");
        scanf("%d", &i);
        printf("Total set bit is : %d\n", countSetBits(i));
        return 0;
}

Output :
Enter The Number : 5
Total set bit is : 2


Binary Representation  of a given Number :

#include <stdio.h>
void binary(unsigned n)
{
        unsigned i;
        for (i = 1 << 31; i > 0; i = i / 2)
                (n & i)? printf("1"): printf("0");
}
int main()
{
        int i;
        printf("Enter The Number : ");
        scanf("%d", &i);
        binary(i);
        printf("\n");
        return 0;
}
Output:

Enter The Number : 15
00000000000000000000000000001111


Turn off the particular bit in a number : 
#include <stdio.h>
void turnOff(unsigned n, unsigned k)
{
        int i;
        i = n & ~(1 << (k-1));
        if (i == n)
                printf("The bit position is already off");
        else
                printf("The number after turning off the particular bit is : %d", i);
}
int main()
{
        unsigned n, k;
        printf("Enter the number : ");
        scanf("%d", &n);
        printf("Enter the bit Position : ");
        scanf("%d", &k);
        turnOff(n, k);
        printf("\n");
        return 0;

Explanation : 
The concept is to turn off that particular bit by using bitwise left shift operator << and do bitwise AND (&) with the given number.
eg: n=15, binary form is : 0000 1111 and k=4
~(1<<(k-1))  = ~(1<<3) = ~(0000 1000) =  1111 0111
(0000 1111) & (1111 0111) = 0000 0111

Output :  
Enter the number : 9
Enter the bit Position : 1
The number after turning off the particular bit is : 8
 

Enter the number : 9
Enter the bit Position : 2
The bit position is already off
 

Enter the number : 15
Enter the bit Position : 4
The no after turning off the particular bit is : 7 


Find Position of only set bit :
Only set bit means it have only one "1" and rest all bits will be "0" in binary representation.

void main()
{
        int i= 0, j=1, count =1;
        printf("Enter the number to find position of set bit : ");
        scanf("%d", &i);
        if(!(i && !(i & (i-1))))
        {
                printf("The number is not the 2 power of n\n");
                return;
        }
        while(!(i & j))
        {
                i = i >> 1;
                count ++;
        }
        printf("Position of set bit is : %d\n", count);
}

Output :
Enter the number to find position of set bit : 16
Position of set bit is : 5

Enter the number to find position of set bit : 5
The number is not the 2 power of n

Explanation : The concept is to start from rightmost bit and one by one check value of every bit.
In another way we can check that given number is power of "2". 


How to add two numbers without using Arithmetic Operator(+, ++, –, -, .. etc) : 

#include <stdio.h>
void sum(int i, int j)
{
        printf("The sum of the number is : ");
        while(j != 0)
        {
                int carry;
                carry = i & j;
                i = i^j;
                j = carry << 1;
        }
        printf("%d", i);
}
void main()
{
        int i,j;
        printf("Enter the numbers : ");
        scanf("%d %d", &i, &j);
        sum(i, j);
        printf("\n");
}

Output :
Enter the numbers : 23 45
The sum of the number is : 68


Explanation : 
Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits.
Above is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.


Subtraction of two number without using arithmetic operation : 

#include <stdio.h>
void subs(int i, int j)
{
        printf("The Subtraction of the number is : ");
        while(j != 0)
        {
                int borrow;
                borrow = (~i) & j;
                i = i^j;
                j = borrow << 1;
        }
        printf("%d", i);
}
void main()
{
        int i,j,k;
        printf("Enter the numbers : ");
        scanf("%d %d", &i, &j);
        subs(i, j);
        printf("\n");
}


Output :
Enter the numbers : 12 3
The Substraction of the number is : 9

Enter the numbers : 45 45
The Substraction of the number is : 0


Explanation :
Like addition, the idea is to use subtractor logic using bitwise operators.
The truth table for the half subtractor is given below.

i        j     Diff     Borrow
0      0       0          0
0      1        1          1
1      0        1          0
1      1        0          0


Multiplication of two numbers without using arithmetic operator and loop :

#include<stdio.h>
/* function to multiply two numbers x and y*/
int multiply(int x, int y)
{
        /* 0  multiplied with anything gives 0 */
        if(y == 0)
                return 0;

        /* Add x one by one */
        if(y > 0 )
                return (x + multiply(x, y-1));

        /* the case where y is negative */
        if(y < 0 )
                return -multiply(x, -y);
}

int main()
{
        int i,j;
        printf("Enter the numbers : ");
        scanf("%d %d", &i, &j);
        printf("Multiplication of numbers is : %d\n", multiply(i, j));
        return 0;
}
Output :

Enter the numbers : 11 5
Multiplication of numbers is : 55

Enter the numbers : 11 -5
Multiplication of numbers is : -55

Enter the numbers : -11 5
Multiplication of numbers is : -55

Enter the numbers : -11 -5
Multiplication of numbers is : 55



No comments:

Post a Comment