Use of "bit" in Delphi (2)-Set

Source: Internet
Author: User
Each element in the Set occupies only one binary bit. A set of less than eight elements only needs one byte.
First observe the size of the Set:
 
Type tset1 = set of (A1, A2, A3, A4, A5, A6, A7, A8); {exactly corresponds to eight bytes} tset2 = set of (B1, b2, B3); {only three digits in one byte need to occupy one byte} tset3 = set of (C1, C2, C3, C4, C5, C6, C7, C8, c9); {9 digits are required, and one byte cannot accommodate} tset4 = set of char; Procedure tform1.button1click (Sender: tobject); begin showmessagefmt ('% d, % d', [sizeof (tset1), // 1 sizeof (tset2), // 1 sizeof (tset3), // 2 sizeof (tset4) // 32]); end;

  

Binary representation of the insight set:

{View binary functions} 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); // returns 111end;

  

You can even think of a set as a number:

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;

  

Redo the previous example in the form of a set (the form design and test results are the same as before ):

VaR set1: Set of 0 .. 7; {prepare to store the following eight States with a custom set variable set1} procedure tform1.formcreate (Sender: tobject); begin checklistbox1.items. commatext: = 'a, B, c, d, e, f, g, H'; button1.caption: = 'Save status'; button2.caption: = 'Restore status'; button3.caption: = 'all select'; button4.caption: = 'all deselected '; 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;

  

Instance observation tfontstyles set:

{View binary functions} 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;

  

Test the tfontstyles set:


Next, we will learn about the tbits class. The "bit" Operation tbits should be the most intuitive.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.