Thread: Bit Reversal

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Bit Reversal

    How can I optimize the bit reversal code to reduce the number of iterations [code] #define NUMER_OF_BITS_IN_BYTE 8 #define SIZE_INT sizeof(int) #define SIZE_CHAR sizeof(char) #define MASK 0x1 #define CHAR int reversebits_int(int x) { int mask = MASK;; int rev = 0 ,pos = 0; while(mask) { rev |= (1

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Code:
    int reversebits_int(int x)
    {
      int rev;
    
      rev  = (x << 31) & 0x80000000;
      rev |= (x << 29) & 0x40000000;
      rev |= (x << 27) & 0x20000000;
    ...
      rev |= (x << 1) & 0x00010000;
      rev |= (x >> 1) & 0x00008000;
      rev |= (x >> 3) & 0x00004000;
    ...
      rev |= (x >> 31) & 0x00000001;
    
      return rev;
    
    }
    This code has the minimal number of iterations possible - since there's no loop, there's 0 of them. Did you have a different question in mind?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Post the code and explain clearly what exactly you are trying to do with.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Screw that inefficient one-bit-at-a-time way of doing it!

    Bit Twiddling Hacks - 'nuf said.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Byte Reversal
    By PureWin in forum C Programming
    Replies: 10
    Last Post: 09-22-2009, 01:24 PM
  2. string reversal
    By roaan in forum C Programming
    Replies: 21
    Last Post: 07-05-2009, 09:16 AM
  3. Recursive string reversal
    By orikon in forum C++ Programming
    Replies: 3
    Last Post: 10-04-2006, 01:35 PM
  4. string reversal
    By anjana in forum C Programming
    Replies: 2
    Last Post: 09-18-2006, 02:42 PM
  5. string reversal
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-14-2002, 12:30 PM

Tags for this Thread