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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The following is pseudocode, don't take it too literal allthough I guess it will probably run ;-)
(* DECLARATION *)VARsize,i:INT;Bitcount:INT:=0;CurBoolVal:BOOL;TheWordIWantToCheck:WORD;END_VAR(* CODE *)size:=SIZE_OF(TheWordIWantToCheck);(* dependends on processor memory width but I assume 16 for now *)FORi:=0TOsize-1DO(* 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 *)END_FOR;(* after this loop has ended, BitCount will contain the counted bits that are TRUE in the word *)
Last edit: hermsen 2021-01-09
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 ;-)
Last edit: hermsen 2021-01-09
https://help.codesys.com/webapp/_cds_operands_variables_accessing_bits;product=codesys;version=3.5.15.0
The index must be a constant.
How about that ...