Join our technical discussions about Freescale Microcontrollers: M68HC12. (Freescale Semiconductor is a Subsidiary of Motorola).
Counting 1's in 16 bit word - AlD - Oct 20 18:55:58 2009
I am trying to count the number of 1's in a sixteen bit word for MC68HC12. The only way I
can think of is to use 16 BITA instructions, one for each bit location, then increment a
counter. I suspect there is a more elegant way to do this. Could someone share their ideas
with me.
------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )
Re: Counting 1's in 16 bit word - Andrei Chichak - Oct 20 19:04:55 2009
INT8U i, count;
count = 0;
for (i = 0; i < 16; i++) {
if ((val %2) != 0) {
count++;
}
val >> 1;
}
Paid programmers have a budget of 50 lines of assembly. I wouldn't
blow my budget on something as simple as this.
A
On 2009-October-20, at 4:55 PM, AlD wrote:
> I am trying to count the number of 1's in a sixteen bit word for
> MC68HC12. The only way I can think of is to use 16 BITA
> instructions, one for each bit location, then increment a counter. I
> suspect there is a more elegant way to do this. Could someone share
> their ideas with me.
---------------------
Andrei Chichak
Systems Developer
CBF Systems Inc.
4-038 NINT Innovation Centre
11421 Saskatchewan Drive
Edmonton, Alberta
Canada
T6G 2M9
Phone: 780-628-2072
Skype: andrei.chichak
[Non-text portions of this message have been removed]
------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )
Re: Counting 1's in 16 bit word - Angel Castillo - Oct 20 20:34:56 2009
si el dato a evaluar esta en el doble acumlador D
y el coneo de unos en Ix
ldx #00
ldy #08
ciclo lsrd
bcc cero
inx
cero dey
bne ciclo
2009/10/20 AlD
> I am trying to count the number of 1's in a sixteen bit word for MC68HC12.
> The only way I can think of is to use 16 BITA instructions, one for each bit
> location, then increment a counter. I suspect there is a more elegant way to
> do this. Could someone share their ideas with me.
>
> ------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )Re: Counting 1's in 16 bit word - AlD - Oct 20 20:45:09 2009
Thanks, but I'm just learning assembly so I have to do it in assembly, any ideas?
--- In 6...@yahoogroups.com, Andrei Chichak
wrote:
>
> INT8U i, count;
>
> count = 0;
> for (i = 0; i < 16; i++) {
> if ((val %2) != 0) {
> count++;
> }
> val >> 1;
> }
>
> Paid programmers have a budget of 50 lines of assembly. I wouldn't
> blow my budget on something as simple as this.
>
> A
> On 2009-October-20, at 4:55 PM, AlD wrote:
>
> > I am trying to count the number of 1's in a sixteen bit word for
> > MC68HC12. The only way I can think of is to use 16 BITA
> > instructions, one for each bit location, then increment a counter. I
> > suspect there is a more elegant way to do this. Could someone share
> > their ideas with me.
> >
> > ---------------------
> Andrei Chichak
>
> Systems Developer
> CBF Systems Inc.
> 4-038 NINT Innovation Centre
> 11421 Saskatchewan Drive
> Edmonton, Alberta
> Canada
> T6G 2M9
>
> Phone: 780-628-2072
> Skype: andrei.chichak
>
> [Non-text portions of this message have been removed]
>
------------------------------------
______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )Re: Counting 1's in 16 bit word - Tom Almy - Oct 20 21:19:50 2009
Try this:
1. Set count to 0
2, Shift value right
3. if carry flag set increment count
4. if value is non-zero, go back to step 2
Tom Almy
Tualatin, Oregon USA
Internet: t...@almy.us
Website: almy.us
On Oct 20, 2009, at 3:55 PM, AlD wrote:
> I am trying to count the number of 1's in a sixteen bit word for
> MC68HC12. The only way I can think of is to use 16 BITA
> instructions, one for each bit location, then increment a counter. I
> suspect there is a more elegant way to do this. Could someone share
> their ideas with me.
>
> ------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )
Re: Counting 1's in 16 bit word - AlD - Oct 20 21:39:16 2009
Great, just what I was looking for, makes sense.
--- In 6...@yahoogroups.com, Angel Castillo
wrote:
>
> si el dato a evaluar esta en el doble acumlador D
> y el coneo de unos en Ix
>
> ldx #00
> ldy #08
> ciclo lsrd
> bcc cero
> inx
> cero dey
> bne ciclo
> 2009/10/20 AlD > I am trying to count the number of 1's in a sixteen
bit word for MC68HC12.
> > The only way I can think of is to use 16 BITA instructions, one for each bit
> > location, then increment a counter. I suspect there is a more elegant way to
> > do this. Could someone share their ideas with me.
> >
> >
> >
> > ------------------------------------
> >
> >

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )Re: Counting 1's in 16 bit word - Andrei Chichak - Oct 20 22:46:30 2009
That's what I said.
BTW, don't let on that this is for a course or people will get really
rude, tell you to read the book, and complain that they aren't getting
a portion of your marks.
A
On 2009-October-20, at 7:19 PM, Tom Almy wrote:
> Try this:
> 1. Set count to 0
> 2, Shift value right
> 3. if carry flag set increment count
> 4. if value is non-zero, go back to step 2
>
> Tom Almy
> Tualatin, Oregon USA
> Internet: t...@almy.us
> Website: almy.us
>
> On Oct 20, 2009, at 3:55 PM, AlD wrote:
>
> > I am trying to count the number of 1's in a sixteen bit word for
> > MC68HC12. The only way I can think of is to use 16 BITA
> > instructions, one for each bit location, then increment a counter. I
> > suspect there is a more elegant way to do this. Could someone share
> > their ideas with me.
> >
> >
> >
> > ------------------------------------
> >
> >

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )
Re: Counting 1's in 16 bit word - Tom Almy - Oct 21 0:09:20 2009
No, there is a fundamental difference -- your algorithm (and Angel
Castillo's) iterate 16 times, while mine only iterates while the value
is non-zero.
I'm also sure that AID is a student (students are most likely to ask
questions like this), so I went no further than suggesting an approach.
Tom Almy
Tualatin, Oregon USA
Internet: t...@almy.us
Website: almy.us
On Oct 20, 2009, at 7:46 PM, Andrei Chichak wrote:
> That's what I said.
>
> BTW, don't let on that this is for a course or people will get really
> rude, tell you to read the book, and complain that they aren't getting
> a portion of your marks.
>
> A
>
> On 2009-October-20, at 7:19 PM, Tom Almy wrote:
>
>> Try this:
>> 1. Set count to 0
>> 2, Shift value right
>> 3. if carry flag set increment count
>> 4. if value is non-zero, go back to step 2
>>
>> Tom Almy
>> Tualatin, Oregon USA
>> Internet: t...@almy.us
>> Website: almy.us
>>
>> On Oct 20, 2009, at 3:55 PM, AlD wrote:
>>
>>> I am trying to count the number of 1's in a sixteen bit word for
>>> MC68HC12. The only way I can think of is to use 16 BITA
>>> instructions, one for each bit location, then increment a counter. I
>>> suspect there is a more elegant way to do this. Could someone share
>>> their ideas with me.
>>>
>>>
>>>
>>> ------------------------------------
>>>
>>>

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )
Re: Counting 1's in 16 bit word - Andrei Chichak - Oct 21 0:20:15 2009
Ah, clever, I like the != 0 trick. You must be from Oregon.
Did you notice the bug in the Spanish code?
A
On 2009-October-20, at 10:09 PM, Tom Almy wrote:
> No, there is a fundamental difference -- your algorithm (and Angel
> Castillo's) iterate 16 times, while mine only iterates while the value
> is non-zero.
>
> I'm also sure that AID is a student (students are most likely to ask
> questions like this), so I went no further than suggesting an
> approach.
>
> Tom Almy
> Tualatin, Oregon USA
> Internet: t...@almy.us
> Website: almy.us
>
> On Oct 20, 2009, at 7:46 PM, Andrei Chichak wrote:
>
> > That's what I said.
> >
> > BTW, don't let on that this is for a course or people will get
> really
> > rude, tell you to read the book, and complain that they aren't
> getting
> > a portion of your marks.
> >
> > A
> >
> > On 2009-October-20, at 7:19 PM, Tom Almy wrote:
> >
> >> Try this:
> >> 1. Set count to 0
> >> 2, Shift value right
> >> 3. if carry flag set increment count
> >> 4. if value is non-zero, go back to step 2
> >>
> >> Tom Almy
> >> Tualatin, Oregon USA
> >> Internet: t...@almy.us
> >> Website: almy.us
> >>
> >> On Oct 20, 2009, at 3:55 PM, AlD wrote:
> >>
> >>> I am trying to count the number of 1's in a sixteen bit word for
> >>> MC68HC12. The only way I can think of is to use 16 BITA
> >>> instructions, one for each bit location, then increment a
> counter. I
> >>> suspect there is a more elegant way to do this. Could someone
> share
> >>> their ideas with me.
> >>>
> >>>
> >>>
> >>> ------------------------------------
> >>>
> >>>

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )
Re: Counting 1's in 16 bit word - Nicholas Merriam - Oct 21 7:48:39 2009
http://graphics.stanford.edu/~seander/bithacks.html
--
Dr. Nicholas Merriam
n...@rapitasystems.com
~|~|~ Rapita Systems Ltd.
http://www.rapitasystems.com/
Tel: +44 (0)1904 56 7747 Fax: +44 (0)1904 56 7719
Visit Rapita Systems at MAE09 (Military & Aerospace Electronics).
November 10th 2009 (Reading, UK)
http://www.RapitaSystems.com/events/MAE09
------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )Re: Counting 1's in 16 bit word - tom_almy2000 - Oct 31 16:56:18 2009
Hmm, for some reason I'm not getting about half of the posts here in my inbox, including
this one I discovered by going to the yahoo site.
--- In 6...@yahoogroups.com, Andrei Chichak
wrote:
>
> Ah, clever, I like the != 0 trick. You must be from Oregon.
>
> Did you notice the bug in the Spanish code?
>
> A
His program:
ldx #00
ldy #08
ciclo lsrd
bcc cero
inx
cero dey
bne ciclo
The count, 8, should of course be 16. However it does run faster that way. :-)
------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )