Counter the number of 1 bits?

This is a discussion on Counter the number of 1 bits? within the C++ Programming forums, part of the General Programming Boards category; >For extra brownie points you could compute the look-up table >beforehand instead of manually bashing it out. For extra leetness ...

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >For extra brownie points you could compute the look-up table
    >beforehand instead of manually bashing it out.
    For extra leetness points, I'll let it slip that this table was code generated for a specific number of bits by a tool that I wrote some time ago, not hand crafted.
    My best code is written with the delete key.

  2. #17
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Good stuff...

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In your quest to let everyone know your penis size is bigger than everyone elses, you missed the entire point of my code. I don't care how you do it. I was simply providing yet another FUN variant. I don't really give a ........ what way you do it. I was just showing another interesting variant.

    Some people are so ... eh.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by quzah
    Code:
    unsigned char count foo( unsigned char b )
    {
        return (!!(b&  1))+(!!(b&  2))+(!!(b&  4))+(!!(b&  8))+
               (!!(b& 16))+(!!(b& 32))+(!!(b& 64)0+(!!(b&128));
    }
    Me too, me too!

    So it would go something like this

    i would have b be an int though

    b = 1111 1111

    (1111 1111 & 1) = 0000 0000
    !(0000 0000) = F F
    !(0000 0000) = 0 0

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. It would go like this:

    1111 1111 & 0000 0001 = 0000 0001
    1111 1111 & 0000 0010 = 0000 0010
    ...
    1111 1111 & 1000 0000 = 1000 0000

    To further:

    1111 1111 & 0000 0001 = 0000 0001
    !0000 0001 = 0000 0000
    !0000 0000 = 0000 0001

    1111 1111 & 0000 0010 = 0000 0010
    !0000 0010 = 0000 0000
    !0000 0000 = 0000 0001

    ! non-zero = 0
    ! 0 = 1

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by quzah
    No. It would go like this:

    1111 1111 & 0000 0001 = 0000 0001
    1111 1111 & 0000 0010 = 0000 0010
    ...
    1111 1111 & 1000 0000 = 1000 0000

    To further:

    1111 1111 & 0000 0001 = 0000 0001
    !0000 0001 = 0000 0000
    !0000 0000 = 0000 0001

    1111 1111 & 0000 0010 = 0000 0010
    !0000 0010 = 0000 0000
    !0000 0000 = 0000 0001

    ! non-zero = 0
    ! 0 = 1

    Quzah
    .

    Oppz i added instead of and..thx man!!!

  7. #22
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    !0000 0010 = 0000 0000
    !0000 0000 = 0000 0001

    ! non-zero = 0
    ! 0 = 1


    One thing I dont understand how the NOT operator can turn a 2 into 0 then using the NOT again it turns the 0 into 1.

    ! (0000 0010) = 1111 1101
    ! (1111 1101) = 0000 0010
    thats wat i thought the NOT operator does
    Last edited by gqchynaboy; 04-04-2005 at 12:13 AM.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you can not believe me, or you can simply try it yourself.
    Code:
    cout << "! 2 is " << !2;
    I don't care either way. I've already explained how it works. Believe me or don't. I really don't care.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by quzah
    Well you can not believe me, or you can simply try it yourself.
    Code:
    cout << "! 2 is " << !2;
    I don't care either way. I've already explained how it works. Believe me or don't. I really don't care.

    Quzah.
    No need for attitude, like i said im working in Assembly Langauge and I guess the NOT operator works differently.

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well how many times do you want me to explain it with you saying it doesn't work that way, and me telling you it does, before you actually believe me? What did you expect me to do, tell you I was lying? You either believe it or don't. Either way, like I said, I don't care.

    However, if you can't use AND OR or shifting, just take modulus of 2, and see if it is 1 or not. If so, increment the "bit count" counter, and then divide the number by two. If you run out of number to divide, you're done.

    But hey, don't take my word for it...

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by samGwilliam
    Let's face it though - my solution is more elegant.
    it may be more elegant, but it breaks the number 1 rule of this thread. NO CONDITIONAL TESTS!.

    edit: HA HA! i didn;t realize there was a page 2
    Last edited by misplaced; 04-04-2005 at 01:29 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  12. #27
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Bytes and Nibbles,

    Anybody feel hungry at this moment in time?


  13. #28
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Quote Originally Posted by misplaced
    it may be more elegant, but it breaks the number 1 rule of this thread. NO CONDITIONAL TESTS!.

    edit: HA HA! i didn;t realize there was a page 2
    Good point, but I thought a loop would be permitted.

  14. #29
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by samGwilliam
    Good point, but I thought a loop would be permitted.
    he used a loop as an example of what he can't use!
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  15. #30
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Quote Originally Posted by misplaced
    he used a loop as an example of what he can't use!
    Whoops...

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Explain Count the number of bits in an int code
    By dnysveen in forum C++ Programming
    Replies: 36
    Last Post: 12-23-2006, 09:39 PM
  2. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 02:21 PM
  3. Number of bits in types
    By TriKri in forum C Programming
    Replies: 7
    Last Post: 12-02-2006, 11:56 AM
  4. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  5. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 10:54 AM

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