Thread: Any Idea ...please help

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    India
    Posts
    14

    Any Idea ...please help

    Hi ...

    I read this question in one magzine.

    2 ints A and B are given, find the no of bits that need to be flipped in A to get B.

    Can any one tell me how to solve this....

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Kindly do not cross post. Since I could not determine whether you intended it to be C, C++ or just a question of an algorithm, it has been moved to the Tech board.

    Here's one possible algorithm: set a counter to 0. Start from the first bit of A, and compare it with the first bit of B. If the bits are different, increment the counter. Repeat for the remaining bits.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    It's the number of 1's in A XOR B.

  4. #4
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    603
    How about xor the two ints, then count the 1 bits in the result. (just as an alternative).
    Visit entropysink.com - It's what your PC is made for!

  5. #5
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    603
    Beat me to it
    Visit entropysink.com - It's what your PC is made for!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. project idea ???
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 06-09-2005, 09:56 AM
  2. Have an idea?
    By B0bDole in forum Projects and Job Recruitment
    Replies: 46
    Last Post: 01-13-2005, 03:25 PM
  3. A little problem about an idea, help please
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2004, 09:52 PM
  4. totally screwed up idea
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-17-2001, 12:09 PM