Tuesday, October 28, 2014

C Operators Pro



Check if a given Integer is Power of 2 using Bitwise Operators

#include
#include
#define num_bits_int (8*sizeof(int))
int power_of_2(unsigned int);

int main()
{
            unsigned int num;

            printf("\nEnter Number");
            scanf("%d", &num);
    power_of_2(num);
}

int power_of_2(unsigned int x)
{
    int i, count = 0, result, shift_num;

    for (i = 0;i <= Num_bits_int;i++)
    {
        shift_num = x >> i;
        result = shift_num & 1;
        if (res == 1)
            count++;
    }
    if (count % 2 == 1)
        printf("Yes");
    else
        printf("No");
        getch();
}

Output

Enter Number128
Yes
Enter Number126
No

Swap two Integers without using Temporary Variables and Bitwise Operations

#include 
#include 
void swap(int *, int *);
 
void main()
{
    int x, y;
    printf("Enter 2 nos: \n");
    scanf("%d %d", &x, &y);
    printf("\nYou have entered x = %d y = %d \n", x, y);
    swap(&x,&y);    // passing the 2 nos to the swap function
}
 
void swap(int *a, int *b)
{
    *a = *a + *b;
    *b = *a - *b;
    *a = *a - *b;
    printf("Swapped . . . .\n"); 
    printf("x = %d y = %d\n", *a, *b); 
    getch();
}

Output
 
Enter 2 nos:
4
7
 
You have entered x=4 y=7
Swapped . . . .
x=7 y=4

Replace Bits in Integer x from Bit Position a to b from another Integer y

#include 
#include 
void changebits(int, int, int, int);
 
int main()
{
    int num1, num2, pos1, pos2;
    clrscr();
    printf("**Replacing the bits in integer x from bit position a to b from another integer y**\n");
    printf("read number 1\n");
    scanf("%x", &num1);
    printf("Read number 2:\n");
    scanf("%x", &num2);
    printf("Read LSB postion:\n");
    scanf("%d", &pos1);
    printf("MSB should always be greater than LSB\n");
    printf("Read MSB position:\n");
    scanf("%d", &pos2);
    changebits(num1, num2, pos1, pos2);
    return 0;
}
 
 
void changebits(int num1, int num2, int pos1, int pos2)
{
    int temp1, temp_1, buffer2, bit1 = 0, bit2 = 0, counter = 0, a = 1;
 
    temp1 = num1;
    temp_1 = num1;
    buffer2 = num2;
    for (;pos1 <= pos2;pos1++)        
    {
        a = 1;
        num1 = temp_1;
        num2 = buffer2;
        while (counter <= pos1)
        {
            if (counter  == pos1)        
                bit1 = (num1&1);    
            counter++;
            num1>> = 1;
        }
        counter = 0;
        while (counter <= pos1)
        {
            if (counter == pos1)
                bit2 = (num2&1);        
            counter++;
            num2 >>= 1;
        }
        counter = 0;
        if (bit1 == bit2);
        else
        {
            while (counter++<pos1)
                a = a << 1;                
            temp1 ^= a;    
        }
        counter = 0;
    }
    printf("the number num1 after shifting the bits is 0x%x\n", temp1); 
getch();
}

Output
 
read number 1
0x11223344
Read number 2:
0x55667788
Read LSB postion:
12
MSB should always be greater than LSB
Read MSB position:
19
The number num1 after shifting the bits is 0x11267344

No comments:

Post a Comment