A Delphi set is a bit field whose bit corresponds to the associated values ββof the elements in your set. For a set of normal numbered types, the bit arrangement is straightforward:
- bit 0 corresponds to a given element with ordinal value 0
- bit 1 corresponds to a given element with ordinal value 1
- etc.
Everything becomes a little interesting when you are dealing with an non-contiguous set or with a set that does not start from scratch. You can do this using the Delphi subband type (example: set of 3..7 ) or using the enumerated types that determine the actual ordinal value for the elements:
type enum=(seven=7, eight=8, eleven=11); EnumSet = set of enum;
In such cases, Delphi will allocate the minimum required number of bytes, which will include all the necessary bits, but will not βshiftβ the bit values ββin order to use less space. In the EnumSet example EnumSet Delphi will use two bytes:
- The first byte will have the 7th bit associated with
seven - The second byte will have bit 0 associated with
eight - The second byte will have bit 3 associated with
eleven
You can see some tests that I did here: Delphi 2009 - error? Adding allegedly invalid values ββto the set
Tests were performed using Delphi 2010, did not repeat them for Delphi XE.
Cosmin prund
source share