Closed Thread
Results 1 to 7 of 7

Thread: Reverse bits in a number

  1. #1
    Pushpa is offline Newbie
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Reverse bits in a number

    Hii..
    Can anyone pls post the program to reverse the bits of a number.

    thank you in advance...

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Reverse bits in a number

    No, but we can help you fix your code. Also, are you reversing bits or digits? Integer, float, other?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Ewe Loon's Avatar
    Ewe Loon is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    49
    Rep Power
    0

    Re: Reverse bits in a number

    I would assume it would be for integers, as floating point numbers would get really messed up

    you would want something like this

    Code:
    	int reverse(int n)
    	{
    		int r=0;
    		int c;
    		for (c=0;c<32;c++)
    		{
    			r=(r<<1)+(n & 1);
    			n=n>>1;
    		}
    		return r;
    	}

  5. #4
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: Reverse bits in a number

    Quote Originally Posted by Ewe Loon View Post
    I would assume it would be for integers, as floating point numbers would get really messed up

    you would want something like this

    Code:
    	int reverse(int n)
    	{
    		int r=0;
    		int c;
    		for (c=0;c<32;c++)
    		{
    			r=(r<<1)+(n & 1);
    			n=n>>1;
    		}
    		return r;
    	}
    This code is bad for a number of reasons.

    1) You assume that integers are 32 bits.

    2) Hence I bet you are assuming that integers are 4 bytes and that a byte is always 8 bits. This is wrong.

    3) You should use C99 styled for loops.

    4) In general this is a bad implementation.

  6. #5
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Reverse bits in a number

    This better?

    Code:
    #include <limits.h>
    
    /*
    if you're using C++ do
    # include <climits>
    */
    
    #define     INT_BITS        sizeof(int) * CHAR_BITS
    
    int reverse(int input)
    {
        int hbit;
        int mask = 1 << (INT_BITS - 1);     /* 0x80000000 for 32-bit ints */
        
        for(unsigned int i = 0; i < INT_BITS; ++i)
        {
            hbit = input & mask;
            input <<= 1;
            input |= hbit;
        }
        
        return input;
    }
    Last edited by dargueta; 03-11-2010 at 09:34 PM. Reason: Added comment
    sudo rm -rf /

  7. #6
    Ewe Loon's Avatar
    Ewe Loon is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    49
    Rep Power
    0

    Re: Reverse bits in a number

    well next time i wont bother

  8. #7
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Reverse bits in a number

    Why?
    sudo rm -rf /

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Counting Number of on or off bits in an integer
    By fayyazlodhi in forum C Tutorials
    Replies: 2
    Last Post: 05-07-2011, 02:33 AM
  2. Bits of introduction
    By Voiden in forum Introductions
    Replies: 6
    Last Post: 10-21-2010, 12:15 PM
  3. Allocation bits
    By Apprentice123 in forum General Programming
    Replies: 2
    Last Post: 12-17-2009, 06:59 PM
  4. Bits
    By dcs in forum C Tutorials
    Replies: 5
    Last Post: 08-29-2009, 02:17 PM
  5. Bits of Fun
    By Khomez in forum C and C++
    Replies: 3
    Last Post: 12-05-2008, 11:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts