Invert and reverse bits.

This is a discussion on Invert and reverse bits. within the C++ Programming forums, part of the General Programming Boards category; Hello, I need to invert and reverse the bits in a one byte value. For example: 00111111 original 11000000 inverted ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    16

    Invert and reverse bits.

    Hello,

    I need to invert and reverse the bits in a one byte value. For example:

    00111111 original
    11000000 inverted
    00000011 reversed

    Here is what I have. It works, but I was wondering if there is a better or faster way.

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int invert(int temp);
    
    int main()
    {
            int a = 0x3f;
            cout << hex << a << endl;
            cout << hex << invert(a) << endl;
    
            return 0;
    }
    
    
    
    int invert(int temp)
    {
            int res         = 0;          //Result
    
            temp = 0xff - temp;     // ones complement?
    
            for(int x = 7; x > 0 ; x--)
            {
                    res = res | (temp & 0x01);
                    temp    = temp  >> 1;
                    res       = res     << 1;
            }
            res = res | (temp & 0x01);
    
            return res;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Inverting bits is easy:

    MyVar = ~MyVar;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    cgoat
    Guest
    There's a ~ operator for this:

    Code:
    int x = 0xA5A5A5A5;
    int y = ~x;
    
    printf("%x:%x\n", x, y);

  4. #4
    cgoat
    Guest
    I didn't read the part about reversing... there's no easy way for that, although it would probably be easiest to use the << and >> operators. The following might work, I didn't test it.

    Code:
    x = 0x0F0F0F0F;
    y = 0;
    for(i=0; i<32; ++i) // All 32 bits
    {
       y |= (x & 0x1);
       y << 1;
       x >> 1;
    }

  5. #5
    cgoat
    Guest
    Ignore that last post. I should probably register so I can edit. I didn't think that through enough.

    Anyway, this program will reverse the bits in a 32-bit number:

    Code:
    #include <cstdio>
    
    int main(int argc, char *argv[])
    {
       int x = 0xF0F0F0F0;
       int y = 0;
       int i;
    
       for(i=0; i<32; ++i)
       {
          y = y << 1;
          y |= (x & 0x1);
          x = x >> 1;
       }
       printf("Y = %x\n", y);
    
       return 0;
    }

  6. #6
    cgoat
    Guest
    Man, am I dumb today. You already had all that...

    Although the shifting of the output needs to be before the OR, so that the last time through it doesn't shift after the OR (the last bit in the output should remain in the last position).

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21