I have been looking for a simple way to add the number of active bits within a word (or byte).
Example of what I need:
00000000 = 0
00100000 = 1
00001000 = 1
00110000 = 2
00101010 = 3
11001100 = 4
11111111 = 8
The easiest way I've worked out uses a large amount of Word Ands and bit shifting.
Example, word A is the test word. B is a holding word. C is the sum.
B = A WAND 0001
C = C+B
Shift left A by 1 bit
Repeat
This will be for an S7.
Thanks in advance.
Doug
Example of what I need:
00000000 = 0
00100000 = 1
00001000 = 1
00110000 = 2
00101010 = 3
11001100 = 4
11111111 = 8
The easiest way I've worked out uses a large amount of Word Ands and bit shifting.
Example, word A is the test word. B is a holding word. C is the sum.
B = A WAND 0001
C = C+B
Shift left A by 1 bit
Repeat
This will be for an S7.
Thanks in advance.
Doug