title: Memory layout of a set
body: <p>How is a <code>set</code> organized in memory in Delphi?</p>
  
  <p>What I try to do is casting a simple type to a set type like</p>
  
  <pre><code>var
    MyNumber : Word;
    ShiftState : TShiftState;
  begin
    MyNumber:=42;
    ShiftState:=TShiftState(MyNumber);
  end;
  </code></pre>
  
  <p>Delphi (2009) won't allow this and I don't understand why. It would make my life a lot easier in cases where I get a number where single bits encode different enum values and I just could cast it like this. Can this be done?</p>
  
  <p>One approach I was going to go for is:</p>
  
  <pre><code>var
    ShiftState : TShiftState;
    MyNumber : Word absolute ShiftState;
  begin
    MyNumber:=42;
  end;
  </code></pre>
  
  <p>But before doing this I thought I'd ask for the memory layout. It's more a feeling than knowlege what I am having right now about this.</p>