Mystery.

This is a discussion on Mystery. within the C Programming forums, part of the General Programming Boards category; Hi, I understand the following program, not that well but at least i know how it works. But i really ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Mystery.

    Hi,

    I understand the following program, not that well but at least i know how it works. But i really can't figure out what it does. Pls give some hints.

    Code:
    #include <stdio.h>
    
    int mystery( unsigned );
    
    int main()
    { 
       unsigned x;
    
       printf( "Enter an integer: " );
       scanf( "%u", &x );
       printf( "\nThe result is %d\n", mystery( x ) );
       getch();
       return 0;
    }
    
    int mystery( unsigned bits )
    { 
       unsigned i, mask = 1 << 31, total = 0;
    
       for ( i = 1; i <= 32; i++, bits <<= 1 )
          if ( ( bits & mask ) == mask )
             ++total;
    
        return !( total % 2 ) ? 1 : 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    It counts the occurences of 1's in the binary equývalent of an unsigned integer and returns 1 if the total number of 1's is odd, 0 for even.
    The experimenter who does not know what he is looking for will not understand what he finds.
    - Claude Bernard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mystery with receiving web sites through winsock
    By maxorator in forum C++ Programming
    Replies: 20
    Last Post: 01-05-2007, 08:56 PM
  2. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 07:23 AM
  3. sdl_opengl mystery crash
    By ichijoji in forum Game Programming
    Replies: 2
    Last Post: 09-16-2004, 11:28 PM
  4. Constructors now allowed a return type: Mystery Error
    By wbeasl in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 01:33 PM
  5. The great mystery
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 08-15-2001, 08:08 PM

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