Searching \ for '[PIC]: Counting Bits' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/time.htm?key=count
Search entire site for: 'Counting Bits'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Counting Bits'
2000\12\15@091005 by Andrew Kunz

flavicon
face
I'm unable to search the PICLIST.COM archive, and Jeff Keyser's archive seems to
be searchable only by header info, and as well all know the subject line rarely
retains much resemblance to the question inside the message.

I'm looking for a nice efficient way to count the number of bits set in an 8-bit
register.  I _know_ there were some nice pieces out there, but I can't seem to
find one.

Anybody have one I can use?

Thanks.

Andy

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2000\12\15@100451 by Morgan Olsson

picon face
Andrew Kunz wrote:
>I'm looking for a nice efficient way to count the number of bits set in an 8-bit
>register.  I _know_ there were some nice pieces out there, but I can't seem to
>find one.

Same here, I *know* Mr Kiryashov made a wonderful solution about two years ago, but I've lost it.

/Morgan

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2000\12\15@100915 by Drew Vassallo

picon face
>I'm looking for a nice efficient way to count the number of bits set in an
>8-bit
>register.  I _know_ there were some nice pieces out there, but I can't seem
>to
>find one.

How about:

CBLOCK 0x0C
  Count
  Loop_Count
ENDC

  clrf Count
  movlw 0x08
  movwf Loop_Count
  clrc
Loop
  rrf Number
  btfsc STATUS, C
  incf Count
  decfsz Loop_Count
  goto Loop

Where "Count" holds the number of bits set.  I don't know about "efficient,"
but I'm not sure how to do it faster offhand.

or, by brute force:

  clrf Count
  btfsc Number, 0
  incf Count
  btfsc Number, 1
  incf Count
  ... etc.

This would actually have fewer instruction cycles, but would take up just
about twice the program memory.

--Andrew
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2000\12\15@101111 by jamesnewton

face picon face
www.piclist.com/techref/microchip/math/bit/index.htm

---
James Newton (PICList Admin #3)
spam_OUTjamesnewtonTakeThisOuTspampiclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org

{Original Message removed}

2000\12\15@111805 by Ray Gardiner
flavicon
face
I remember Dmitry had a **very** fast way of doing this, but I can't find it
just now. It's probably several generations of computers ago.
However there is a good collection of bit counting methods at:-

       http://www.dattalo.com/technical/software/pic/picprty.html


{Quote hidden}

Ray Gardiner .....rayKILLspamspam@spam@dsp.com.au
mail from: dsp systems

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2000\12\15@114327 by Scott Dattalo

face
flavicon
face
On Sat, 16 Dec 2000, Ray Gardiner wrote:

> I remember Dmitry had a **very** fast way of doing this, but I can't find it
> just now. It's probably several generations of computers ago.
> However there is a good collection of bit counting methods at:-
>
>         http://www.dattalo.com/technical/software/pic/picprty.html

The ones there are slugs compared to Dmitry's.

Scott

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2000\12\15@114526 by Ray Gardiner

flavicon
face
Ok, here is the one I was thinking of. It is archived at
http://www.piclist.com/techref/microchip/math/bit/countbits.htm

Should have looked on http://www.piclist.com first!..


Dmitry's amazing code follows....


; Less program memory requiring solution
; 14 clock per convertation but only
; 14 words of program memory is required

       movfw   portX
       movwf   temp_w

       andlw   0x77
       addwf   temp_w,f
       rrf     temp_w,f

       andlw   0x33
       addwf   temp_w,f
       rrf     temp_w,f

       andlw   0x11
       addwf   temp_w,f
       rrf     temp_w,f

       swapf   temp_w,w
       addwf   temp_w,w
       andlw   0x0F




Ray Gardiner rayspamKILLspamdsp.com.au
mail from: dsp systems

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2000\12\15@114802 by Andrew Kunz

flavicon
face
Thanks folks.

I'm using Dmitry's excellent 14-word version.

Andy

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2000\12\15@124205 by Bill Westfield

face picon face
Here's what I found in my archive - no message from Dmitry, though -
presumably he had the referenced 13 cycle version...

BillW

   Date:         Wed, 22 Jul 1998 12:04:33 +0800
   From: Regulus Berdin <.....rberdinKILLspamspam.....SKYINET.NET>
   Subject:      Re: Bit counting (looking for old challenge emails)
   To: EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU

   Hi to all,

   > From: Scott Dattalo <sdattalospamspam_OUTUNIX.SRI.COM>
   [SNIP]
   > Thanks for the plug Bill. However, the routines on my web page are by no
   > means the fastest (as I've been shown). One PICLIST member has an
   > isochronous bitcount routine that runs in 13 cycles. So unless he wishes
   > to post it, I guess there's a challenge for someone out there.

   I took the challenge, here is what I got.

   BITCOUNT:               ;untested
         RRF   Q,W         ;Q = abcdefgh
         MOVWF T           ;T = 0abcdefg
         MOVLW B'01010101'
         ANDWF Q           ;Q = 0b0d0f0h
         ANDWF T,W         ;W = 0a0c0e0g
         ADDWF Q           ;Q = (a+b)(c+d)(e+f)(g+h)

         RRF   Q,W
         MOVWF T
         RRF   T           ;T = (xx)(a+b)(c+d)(e+f)
         MOVLW B'00110011'
         ANDWF Q           ;Q = (00)(c+d)(00)(g+h)
         ANDWF T,W         ;W = (00)(a+b)(00)(e+f)
         ADDWF Q           ;Q = (a+b+c+d)(e+f+g+h)

         SWAPF Q,W         ;W = (e+f+g+h)(a+b+c+d)
         ADDWF Q,W         ;W = (a+b+c+d+e+f+g+h)(a+b+c+d+e+f+g+h)
         ANDLW H'0F'       ;W = (0000)(a+b+c+d+e+f+g+h)

   Data is in Q and result is placed in W.

   The routine is isochronous but 16 cycles. I cannot shave
   it down to 13 or less :(.  I wonder how this piclist member
   did it or what method/algorithm he used.

   Reggie

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2000\12\15@130452 by jamesnewton

face picon face
I just realized that Andy was apparently searching the iversoft archive and
thinking that it was the new piclist.com archive. The iversoft archive is at
http://www2.iversoft.com/piclist
and appears to be down. The new piclist.com archive is at
http://www.piclist.com/techref/postbot.asp
It is working just fine. You must be a piclist.com member (not just a
piclist member) to access it.

The code Andy is looking for is at
www.piclist.com/techref/microchip/math/bit/countbits.htm
and there are many other bit related snippets as well at
www.piclist.com/techref/microchip/math/bit/index.htm
and a ton of code at
www.piclist.com/techref/microchip/routines.htm
nicely indexed by subject

---
James Newton (PICList Admin #3)
@spam@jamesnewtonKILLspamspampiclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org

{Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...