Delphi 中 “位” 的使用(2) – 集合

來源:互聯網
上載者:User
集合中的每個元素其實只佔一個二進位位, 不足 8 個元素的集合只需要 1 個位元組.
先觀察集合的大小:
Type  TSet1 = set of (a1,a2,a3,a4,a5,a6,a7,a8);    {剛好對應一個位元組的 8 個位}  TSet2 = set of (b1,b2,b3);                   {只用一個位元組中的 3 個位, 也要佔一個位元組}  TSet3 = set of (c1,c2,c3,c4,c5,c6,c7,c8,c9); {需要 9 個位, 一個位元組容不下了}  TSet4 = set of Char;procedure TForm1.Button1Click(Sender: TObject);begin  ShowMessageFmt('%d, %d, %d, %d', [SizeOf(TSet1), //1                                    SizeOf(TSet2), //1                                    SizeOf(TSet3), //2                                    SizeOf(TSet4)  //32                                   ]);end;

洞察集合的二進位表示:

{查看二進位的函數}function ToBin(p: PByteArray; b: Integer): string;var  i,j: Integer;begin  Result := StringOfChar('0', b * 8);  for i := 0 to b - 1 do for j := 0 to 7 do    if Odd(p^[b-1-i] shr j) then Result[i*8 + 8 - j] := '1';end;procedure TForm1.Button1Click(Sender: TObject);var  set1: set of (A,B,C,D,E,F,G,H);begin  set1 := [];  ShowMessage(ToBin(@set1, SizeOf(set1))); //00000000  set1 := [A,B,C,D,E,F,G,H];  ShowMessage(ToBin(@set1, SizeOf(set1))); //11111111  set1 := [A,B,C];  ShowMessage(ToBin(@set1, SizeOf(set1))); //00000111  set1 := [A,B,C,H];  ShowMessage(ToBin(@set1, SizeOf(set1))); //10000111end;

甚至可以把集合看成一個數字:

procedure TForm1.Button1Click(Sender: TObject);type  TSet = set of (A,B,C,D,E,F,G,H);var  s1,s2,s3,s4: TSet;begin  s1 := [];  s2 := [A,B,C,D,E,F,G,H];  s3 := [A,B,C];  s4 := [A,B,C,H];  ShowMessage(IntToStr(Byte(s1))); //0  ShowMessage(IntToStr(Byte(s2))); //255  ShowMessage(IntToStr(Byte(s3))); //7  ShowMessage(IntToStr(Byte(s4))); //135end;

用集合的方式重新做前一次的例子(表單設計與測試效果同前):

var set1: set of 0..7; {準備用自訂的集合變數 set1 儲存下面的 8 種狀態}procedure TForm1.FormCreate(Sender: TObject);begin  CheckListBox1.Items.CommaText := 'A,B,C,D,E,F,G,H';  Button1.Caption := '儲存狀態';  Button2.Caption := '恢複狀態';  Button3.Caption := '全選';  Button4.Caption := '全不選';  Button1.Tag := 1;  Button2.Tag := 2;  Button3.Tag := 3;  Button4.Tag := 4;  Button2.OnClick := Button1.OnClick;  Button3.OnClick := Button1.OnClick;  Button4.OnClick := Button1.OnClick;end;procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;begin  if TButton(Sender).Tag = 1 then set1 := [];  for i := 0 to CheckListBox1.Count - 1 do    case TButton(Sender).Tag of      1: if CheckListBox1.Checked[i] then Include(set1, i);      2: CheckListBox1.Checked[i] := i in set1;      3: CheckListBox1.Checked[i] := True;      4: CheckListBox1.Checked[i] := False;    end;end;

執行個體觀察 TFontStyles 集合:

{查看二進位的函數}function ToBin(p: PByteArray; b: Integer): string;var  i,j: Integer;begin  Result := StringOfChar('0', b * 8);  for i := 0 to b - 1 do for j := 0 to 7 do    if Odd(p^[b-1-i] shr j) then Result[i*8 + 8 - j] := '1';end;procedure TForm1.Button1Click(Sender: TObject);var  fs: TFontStyles;begin  Font.Style := [fsBold, fsItalic, fsUnderline];  fs := Font.Style;  Text := ToBin(@fs, SizeOf(fs));end;procedure TForm1.Button2Click(Sender: TObject);var  fs: TFontStyles;begin  Font.Style := [];  fs := Font.Style;  Text := ToBin(@fs, SizeOf(fs));end;

TFontStyles 集合的測試:


接下來學習 TBits 類; 對 "位" 的操作 TBits 應該是最直觀的.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.