恭喜,你发布的帖子
发布于 2024-06-09 21:36:55
16楼
来看看这个
FUNCTION "BIT_COUNT" : Int
TITLE = 'BIT_COUNT'
{ S7_Optimized_Access := 'FALSE' }
AUTHOR : hugo
FAMILY : GATE
NAME : BITCT
VERSION : 1.1
// Automatically Edited - Old code marked with //~ - (UTC) 10/2/2012 5:11:58 PM
//
//BIT_COUNT counts the amount True of bits in a dword.
//for exabple: bit_count(3) returns 2 because two bits (bits 0 and 1) are true and all others are false.
VAR_INPUT
IN_DW : DWord;
END_VAR
VAR_TEMP
temp : DWord;
attemp AT temp : Array[0..31] of Bool;
END_VAR
BEGIN
#BIT_COUNT := 0;
#temp := #IN_DW;
WHILE DWORD_TO_DINT(#temp) <> 0 DO
IF #attemp[24] THEN
#BIT_COUNT := #BIT_COUNT + 1;
END_IF;
#temp := SHR(IN := #temp, N := 1);
END_WHILE;
(* revision history
5.7.2007 rev 1.0 original version
10.9.2007 rev 1.1 hm
changed algorithm for better performace
the execution time has reduced by a factor of 5
deleted unused variable temp
*)
END_FUNCTION
请填写推广理由:
分享
只看
楼主