Q:

C program to reverse bits of an integer number.

0

C program to reverse bits of an integer number.

This program will reverse all bits of an integer number, we will implement this program by creating a User Define Function, that will return an integer number by reversing all bits of passed actual parameter (integer number).

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Reversing bits of a number using C program

/*C program to reverse bits of a number */

#include <stdio.h>

unsigned int revBits(unsigned int data)
{
    unsigned char totalBits = sizeof(data) * 8;
    unsigned int revNum = 0, i, temp;

    for (i = 0; i < totalBits; i++) {
        temp = (data & (1 << i));
        if (temp)
            revNum |= (1 << ((totalBits - 1) - i));
    }

    return revNum;
}

int main()
{
    unsigned int num = 0x4;
    printf("\n%u", revBits(num));
    return 0;
}
    8912

Note: Output may different based on the compilers.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C solved programs/examples on Bitwise Operators

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to swap two words/bytes.... >>
<< C program to counter number of 1\'s in an int...