Counting 1's in 16 bit word

View: New views
11 Messages — Rating Filter:   Alert me  

Counting 1's in 16 bit word

by a92651delgado :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.


Re: Counting 1's in 16 bit word

by Andrei Chichak-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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]


Re: Counting 1's in 16 bit word

by eltachonder :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <al_delgado@...>

> 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.
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>


[Non-text portions of this message have been removed]


Re: Counting 1's in 16 bit word

by a92651delgado :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks, but I'm just learning assembly so I have to do it in assembly, any ideas?

--- In 68HC12@..., Andrei Chichak <groups@...> 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]
>



Re: Counting 1's in 16 bit word

by Tom Almy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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: tom@...
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.
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>


Re: Counting 1's in 16 bit word

by a92651delgado :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Great, just what I was looking for, makes sense.

--- In 68HC12@..., Angel Castillo <eltachonder@...> 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 <al_delgado@...>
>
> > 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.
> >
> >
> >
> > ------------------------------------
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
>
>
> [Non-text portions of this message have been removed]
>



Re: Counting 1's in 16 bit word

by Andrei Chichak-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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: tom@...
> 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.
> >
> >
> >
> > ------------------------------------
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
>
>

---------------------
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]


Re: Counting 1's in 16 bit word

by Tom Almy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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: tom@...
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: tom@...
>> 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.
>>>
>>>
>>>
>>> ------------------------------------
>>>
>>> Yahoo! Groups Links
>>>
>>>
>>>
>>>
>>
>>
>
> ---------------------
> 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]
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>


Re: Counting 1's in 16 bit word

by Andrei Chichak-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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: tom@...
> 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: tom@...
> >> 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.
> >>>
> >>>
> >>>
> >>> ------------------------------------
> >>>
> >>> Yahoo! Groups Links
> >>>
> >>>
> >>>
> >>>
> >>
> >>
> >
> > ---------------------
> > 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]
> >
> >
> >
> > ------------------------------------
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
>
>

---------------------
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]


Parent Message unknown Re: Counting 1's in 16 bit word

by Nicholas Merriam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

http://graphics.stanford.edu/~seander/bithacks.html 
<http://graphics.stanford.edu/%7Eseander/bithacks.html>

--
Dr. Nicholas Merriam
nmerriam@...
~|~|~   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

Re: Counting 1's in 16 bit word

by Tom Almy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 68HC12@..., Andrei Chichak <groups@...> 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. :-)