×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

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).

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;
}

Output

8912

Note: Output may different based on the compilers.

C Bitwise Operators Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.