Thread: Counter the number of 1 bits?

1. >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.

2. Good stuff...

3. 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.

4. 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. 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.

6. 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
.

7. !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

8. 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.

9. 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. 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.

11. 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

12. Bytes and Nibbles,

Anybody feel hungry at this moment in time?

13. 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. 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!

15. Originally Posted by misplaced
he used a loop as an example of what he can't use!
Whoops...