Home » C programs » C programs on Bitwise Operators

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

    8912





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.