Hi Every body,
I'm pretty new here and i'm trying to put in place a function that will count the number of bit that are SET in a word or a Dword.
Could some body help me ?
Thk in advance
Do yo know the Bitoperator, for examaple Byte.0, Byte.1, or Word.0... Word.15 ? You could build a loop and, depending on the state of the Bitstate, count the state of the Bits.
The following is pseudocode, don't take it too literal allthough I guess it will probably run ;-)
(* DECLARATION *)
size, i : INT;
Bitcount : INT := 0;
CurBoolVal : BOOL;
TheWordIWantToCheck : WORD;
(* CODE *)
size := SIZE_OF(TheWordIWantToCheck); (* dependends on processor memory width but I assume 16 for now *)
FOR i := 0 TO size - 1 DO (* eg. count bit 0 to 15 *)
CurBoolVal := TheWordIWantToCheck.i; (* Helper to cast Bit i to Bool *)
BitCount := BitCount + BOOL_TO_INT(CurBoolVal); (* cast Bit i to Bool *)
(* after this loop has ended, BitCount will contain the counted bits that are TRUE in the word *)
The index must be a constant.
How about that ...
Log in to post a comment.