count bits, fun fun fun. 
Author Message
 count bits, fun fun fun.

Hi!

A while ago there was some clever count-bits programs here. I just did this
code so don't blame me if it isn't the fastest or was posted before.

\ Returns the number of 1's in a number.
: countbits ( n1 -- n2 )
   0 swap
   begin
      dup 0= not
   while
      swap 1+ swap
      dup 1- and
   repeat
   drop
;

Nice, isn't it?

   -Tommy Hallgren-




Wed, 19 Feb 1997 20:47:00 GMT  
 count bits, fun fun fun.
Quote:
Tommy Hallgren writes:
> > A while ago there was some clever count-bits programs here. I just did
> thiscode so don't blame me if it isn't the fastest or was posted before.
> > \ Returns the number of 1's in a number.
> : countbits ( n1 -- n2 )
>    0 swap
>    begin
>       dup 0= not
>    while
>       swap 1+ swap
>       dup 1- and
>    repeat
>    drop
> ;
> I do like that. May I tho? <fiddles around a bit> If you substitute dup

0= not with ?dup you can drop the drop :

: countbits     \ n1 -- n2
        0 swap
        begin
                ?dup            \ dups only if val NOT 0
        while
                swap 1+ swap
                dup 1- and
        repeat
;

TonyR-A



Wed, 26 Feb 1997 14:04:15 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Fun with Clients

2. Fun APL/J Programming Problem

3. roman and arabic - some fun(ctions)

4. Fun trivia question. :-)

5. Fun OTMA IMS exits

6. new instruction (fun)

7. Fun with MS Agent

8. Fun method for the day

9. Fun trick summing letters

10. Fun trick summing letters but?

11. Just for fun

12. Y2K Fun

 

 
Powered by phpBB® Forum Software