Write a C function called setbits no need to write a fullfle

Write a C function, called set_bits(), (no need to write a full-fledged program, just the function) that takes three arguments: an unsigned int x, and unsigned int 1, and an unsigned int r. The function sets the bits from 1 to r (inclusive) in x to 1. Remember that an integer is 32 bits, starting from the bit 0 (in the right) till bit 31 (in the left).

Solution

unsigned int reverseBits(unsigned int num)
{
    unsigned int NO_OF_BITS = sizeof(num) * 8;
    unsigned int reverse_num = 0, i, temp;

    for (i = 0; i < NO_OF_BITS; i++)
    {
        temp = (num & (1 << i));
        if(temp)
            reverse_num |= (1 << ((NO_OF_BITS - 1) - i));
    }

    return reverse_num;
}

int main()
{
    unsigned int x = 2;
    printf(\"%u\", reverseBits(x));
    getchar();
}

 Write a C function, called set_bits(), (no need to write a full-fledged program, just the function) that takes three arguments: an unsigned int x, and unsigned

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site