# PICMicrocontollerBitMathMethod

## Counting how many bits are set

Dmitry Kiryashov [zews at AHA.RU] says

Below the examples of convertation of 8 bits input value to bit sum.
```; On fly solution
; 7 clocks per convertation , 256 bytes for table is required

movfw   portX
call    bit_cnt
;...
bit_cnt:
;256 bytes of data at all
dt      0,1,1,2,1,2,2,3,.....

; 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
rrf     temp_w,f

andlw   0x33
rrf     temp_w,f

andlw   0x11
rrf     temp_w,f

swapf   temp_w,w
andlw   0x0F

According to your requirements you will choose the right way.
Good luck ;-)

```

Brian Scearce [bls at BEST.COM] says

```bits_set_in_lower_half:
andlw   0x0f
retlw   0       ; 0000
retlw   1       ; 0001
retlw   1       ; 0010
retlw   2       ; 0011
retlw   1       ; 0100
retlw   2       ; 0101
retlw   2       ; 0110
retlw   3       ; 0111
retlw   1       ; 1000
retlw   2       ; 1001
retlw   2       ; 1010
retlw   3       ; 1011
retlw   2       ; 1100
retlw   3       ; 1101
retlw   3       ; 1110
retlw   4       ; 1111

movf    portX, w
movwf   tmp_w
call    bits_set_in_lower_half
movwf   sum
swapf   tmp_w, w
call    bits_set_in_lower_half
```

This is both slower (19 clocks vs. 14) and larger (25 words vs. 14) than Dmitry Kiryashov's amazing solution to the problem, but it does have the advantage of clarity.

There's also the trick that "X & (X-1)" clears the lowest set bit on X; you might be able to take advantage of that, especially if you can fix the threshold number of bits. I don't have my PIC book with me, pardon me if I get tests backward or forget which instructions can set the Z-flag, but I think you can get the gist from this code:

```        clrf    sum
movf    portX, w
movwf   tmp_w
loop:
btfsc   status, z
goto   done
incf    sum, f
decf    tmp_w, f
andwf   tmp_w, w
movwf   tmp_w
goto    loop
done:
```

Only ten instructions, but it takes longer the more 1's there are. If you can fix the threshold number of 1's, you can make it better. Say you require at least three bits be set:

```        movlw   3
movwf   count
movf    portX, w
movwf   tmp_w
loop:
btfsc   status, z
goto   not_enough_set
decf    tmp_w, f
andwf   tmp_w, w
movwf   tmp_w
decfsz  count, f
goto   loop
goto    enough_set
```

Twelve instructions, but you save two instructions by not needing to do a compare and test after the number of set bits has been calculated, and this one will finish early if possible.

And, of course, if the threshold is fixed and high enough, you can do it with inlined code:

```        comf    portX, w
btfsc   status, z
goto   eight_bits_set
movwf   tmp_w
andwf   tmp_w, w
btfsc   status, z
goto   seven_bits_set
fewer_than_seven_bits_set:
```

• TakeThisOuTferrous2 at SPAMjuno.com
For what it's worth, it can be done in nine lines and 51 cycles.

clrf sum
movlw 08
moxwf count
loop rrf portx
btfsc status ,c
incf sum
decfsz count
goto loop
rrf portx

Far from elegant, and the highest average cycle count (but constant).
+

 file: /Techref/microchip/math/bit/countbits.htm, 4KB, , updated: 2004/12/6 12:57, local time: 2022/8/16 18:15, TOP NEW HELP FIND:  3.236.225.157:LOG IN

 ©2022 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! PIC Microcontoller Bit Math Method - Counting how many bits are set

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

.