CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Location
    New York
    Posts
    8

    Some bitwise problems..

    I have a short (16bits) storing true and false (1 and 0) in the lower 9 bits, and I want to be able to count the number of 1s (or 0).
    Example: 0000000010111010 (answer would be 5 in this case, total # of 1s)

    This is what I came up with(assuming n is the value and total is the amount of 1s):

    Code:
    int i = 0, total = 0;
    while(i++ < 9)
        total += 1 - ~((n >>> 1) | 0xFFFE);
    Is there a better way to do it in O(1) instead of O(n)?

    Thanks in advance.
    Last edited by VolatileObject; April 11th, 2009 at 10:36 PM.

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Some bitwise problems..

    This gentlemen has some pretty good algorithms to compute the number of 1-bits in O(log n) machine instructions for you on his site:

    http://gurmeetsingh.wordpress.com/20...ting-routines/

    http://infolab.stanford.edu/~manku/bitcount/bitcount.c
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: Some bitwise problems..

    I don't believe there is an O(1) solution, but there is an O(number of ones) algorithm involving repeatedly eliminating the least significant bit:

    Code:
    short int oneBits(short int n) {
      short int total;
    
      if (!n) return 0;
    
      for (total = 1; n ^= ((n - 1) ^ n) & n; ++total);
    
      return total;  
    }
    To break it down, it works like this:

    • let n = 010111010b
    • n - 1 = 010111001 // subtract 1 to change all the bits up to the bit we're interested in
    • (n - 1) ^ n = 000000011 // XOR it with the original value to get only the changed bits (those up to and including the interesting bit)
    • ((n - 1) ^ n) & n = 0000000010 // AND that with the original value to set only the interesting bit
    • n ^ (((n - 1) ^ n) & n) = 010111000 // XOR that with the original value to flip the interesting bit
    • if the new value of n is 0, we have no more 1 bits left, so exit


    Edit: ah! There's a simpler algorithm to do the same thing in the top link in the post above (by ahoodin). Silly that I didn't see that *learns*
    Last edited by Twey; April 11th, 2009 at 11:48 PM.

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Some bitwise problems..

    This solution is still O(n). There is a for loop. The op was looking for something a little better.
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: Some bitwise problems..

    Yes, but n is the number of one bits, not the total number of bits — a significant improvement.

  6. #6
    Join Date
    Jan 2009
    Posts
    596

    Re: Some bitwise problems..

    Quote Originally Posted by VolatileObject View Post
    I have a short (16bits) storing true and false (1 and 0) in the lower 9 bits, and I want to be able to count the number of 1s (or 0).
    Example: 0000000010111010 (answer would be 5 in this case, total # of 1s)

    This is what I came up with(assuming n is the value and total is the amount of 1s):

    Code:
    int i = 0, total = 0;
    while(i++ < 9)
        total += 1 - ~((n >>> 1) | 0xFFFE);
    Is there a better way to do it in O(1) instead of O(n)?

    Thanks in advance.
    Well, as you are restricting the input bit string to a mazimum of 16 bits, then technically your solution is O(1). I.e. there is some constant k such that the running time of your solution is always <k.

    In fact, for any algorithm where the possible inputs are restricted to a finite set, there is an O(1) solution. To see this, just run through all possible inputs, find the one which takes the maximum time (and call this time T_max). Then the algorithm run time will be bound by T_max for all possible inputs, making it O(T_max).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured