Please note: We are aware of an issue affecting replies on the Arm Community forums, which may not be loading as expected.

We apologize for any inconvenience and appreciate your patience while we investigate and work to resolve the issue.

Thank you for your understanding.


This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Counting modified bits in a 32bit data word

I have to count modified bits in a volatile 32 bit data word.

Example:
Initial counter32[32] := 0; olddata := 0x00000010; newdata := 0x0000000001;

So we have moddata := newdata xor olddata => moddata := 00000011.

So bit0 and bit1 have changed.
Now I would have counter[1]++ and counter[0]++;

How this can be done on a Cortex-M0+ fastes way?

Best regards Juergen

Parents
  • So bit0 and bit1 have changed.
    Now I would have counter[1]++ and counter[0]++;

    Sorry for that, ok, I try again…

    I have to count bit changes in a 32bit data word, separated for each bit.
    Therefore I have 32 distinct counters in an array named counter32[32].

    counter32[0] counts bit changes of bit0, counter32[1] counts bit changes of bit1, and so on.

    Every time a bit changes, its counter should be incremented.
    olddata := 0x00000002; newdata := 0x00000001;
    olddata ^ newdata gives 0x00000011, so two bits changed.
    Here two bits have changed, bit0 and bit1, so counter32[0]++ and counter32[1]++

    Best regards
    Juergen

Reply
  • So bit0 and bit1 have changed.
    Now I would have counter[1]++ and counter[0]++;

    Sorry for that, ok, I try again…

    I have to count bit changes in a 32bit data word, separated for each bit.
    Therefore I have 32 distinct counters in an array named counter32[32].

    counter32[0] counts bit changes of bit0, counter32[1] counts bit changes of bit1, and so on.

    Every time a bit changes, its counter should be incremented.
    olddata := 0x00000002; newdata := 0x00000001;
    olddata ^ newdata gives 0x00000011, so two bits changed.
    Here two bits have changed, bit0 and bit1, so counter32[0]++ and counter32[1]++

    Best regards
    Juergen

Children
  • I think something like this.
    But this is tooo slow for our needs and I have no knowledge of ARM Assembler...
    MCU is a STM32G0 with Cortex-M0+.

    uint32_t counter32[32];
    olddata = 0x00000001U;
    newdata = 0x00000002U;
    
    void CountBits(uint32_t old, uint32_t new)
    {
        uint32_t x;
        uint32_t mask = 0x00000001U;
        int      i;
    
        x = old ^ new;			// x == 0x00000003
        for (i = 0; i < 32; i++)
        {
            if (x & mask)
                counter[i]++;
            mask <<= 1;
        }        
    }
    
    
    void main (void)
    {
        CountBits (olddata, newdata);
    }