Any Idea ...please help

This is a discussion on Any Idea ...please help within the Tech Board forums, part of the Community Boards category; Hi ... I read this question in one magzine. 2 ints A and B are given, find the no of ...

  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
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    605
    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
    605
    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, 02: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

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