Thread: Bits

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Bits

    I was searching for an algorithm to reverse the bits...
    I found one and coded it in c++...will this work
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    unsigned int num;
    cin>>num;            // Reverse the bits in this number. 
    unsigned int temp = num;     // temp will have the reversed bits of num. 
    
    int i; 
    char xxx[10];
    for (i = (sizeof(num)*8-1); i; i--) 
    {   
     temp = temp | (num & 1); 
    
     temp <<= 1; 
     
     num  >>= 1; 
     
    } 
    
    temp = temp | (num & 1); 
    
    cout<<temp<<"final";
    return 0;
    }

  2. #2
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72
    First of all, read more about bits, then you will see if your code will work or not.
    Wikipedia - Bit
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: [email protected]
    ICQ: 424738586
    AIM: nathanjava

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    Quote Originally Posted by nathanpc View Post
    First of all, read more about bits, then you will see if your code will work or not.
    Wikipedia - Bit
    S i do know the basics... but here in teh code pasted num value is divided by 2 at each iteration and reaches zero...(since right shift)
    temp value multiplying all the time(since leftshift)
    i don't see anything for reversal as far as i know
    that is why am here

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dpp View Post
    temp value multiplying all the time(since leftshift)
    i don't see anything for reversal as far as i know
    that is why am here
    forget about multiplication and division here

    we just moving bits from one number to the right - and taking every time the least significant one, then moving it to another number, shifting what is there to the left...

    look at it as if you have 32 blocks with 0 or 1 written on it - you are taking one from the right and adding it to another column...

    when you are finished - you have same 32 blocks ordered in the opposite direction
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
    // reverse bits in a byte
    unsigned char A, B;
    
    A = 170; // the number to be reversed
    B = 0;
    
    for(int x = 0;x<8;x++){
       B << 1;
       B += A & 1;
       A >> 1;
       }

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    Quote Originally Posted by vart View Post
    forget about multiplication and division here

    we just moving bits from one number to the right - and taking every time the least significant one, then moving it to another number, shifting what is there to the left...

    look at it as if you have 32 blocks with 0 or 1 written on it - you are taking one from the right and adding it to another column...

    when you are finished - you have same 32 blocks ordered in the opposite direction
    Thanks for the reply...
    I don't undesrtand the code with your explanation completely...
    will u be patient enough to explain me in detail
    Thousands of thanks

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    In c++ the >> is the bitshift operator. While it is true that a bitshift is equivalent to a / or * by 2 in binary, it should not be thought of that way.

    For example lets say that
    short x = 8;
    short y = 9;

    thats like saying
    x = 0x1000
    y = 0x1001

    x >> 1 is
    0x0100
    x >> 2 is
    0x0010
    x >> 3 is
    0x0001

    and y << 1 is
    0x0010
    y << 2 is
    0x0100
    and y << 3 is
    0x1000

    notice how it is shifting. Maybe to be more expressive, a base ten shift would look like this.

    234 << 1 = 340
    234 >> 1 = 023

    effectively dividing and multiplying the number by 10.

    hope that makes sense.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    For more practical purposes, I prefer this method:
    Bit Twiddling Hacks
    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. merging bits
    By Drac in forum C++ Programming
    Replies: 8
    Last Post: 12-21-2008, 06:13 PM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  4. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  5. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM