Delphi编译器中唯一与SET相关的过程是 Include , Exclude 和 In -没有 Count SET的方法。
假设我具有以下枚举和枚举集:
type
TLocation = (locHere, locThere, locElsewhere);
TLocations = Set of TLocation;
后来我在TLocations枚举集中添加了两个TLocation枚举
...
var locs: TLocations;
...
begin
Include(locs, locHere);
end;
现在,我想计算集合中有多少个枚举,这是我发现的摘录,但是我不知道如何称呼它:
function CountSetItems(SetValue: Integer): Byte;
var
Mask: Integer;
begin
Mask := $80000000;
Result := 0;
while Mask <> 0 do begin
if SetValue and Mask <> 0 then
inc(Result);
Mask := Mask shr 1;
end;
end;