Results 1 to 3 of 3

Thread: Convert set to binary representation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Maybe like this?

    Code:
    procedure Foo;
    var
    	Mask, Index: LongWord;
    	s: TMySet;
    begin
    	s := [mtOne, mtTwo, mtSixteen];
    	Index := 1;
    	Mask := 0;
    	while Index <= Integer(High(TMyType)) do
    	begin
    		if TMyType(Index) in s then
    			Mask := Mask or Index;
    		Index := Index * 2;
    	end;
    end;

  2. #2
    You guys are overdoing it a bit. Delphi already stores sets in the form of flags (only difference is that it uses 16 bytes of memory instead of 4 byte dword), so all you need to do to get your flags is type cast the set to a dword like this:
    Flags := LongWord(AnySet);
    if you want this to work you will have to use enumeration range from 0 to 31 (type TMyEnum = (meOne, meTwo{these are fine}, meThrityThree = 32{this will be out of range for a dword})

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •