Use of "bit" in Delphi (1)-original binary

Source: Internet
Author: User
This topic involves: 1. Common binary bit Operations; 2. Relationship between sets and "bits"; 3. tbits class.

Here the "bit" refers to the binary bit. For example, a byte has eight digits and an integer has 32 digits.

The structure composed of "bit" fields can be defined in C, but it seems that it is not widely used due to efficiency issues.

If you want to save eight States (true/false), the most stupid way is to use the following struct:

 
Type tstate = record B1, B2, B3, B4, B5, B6, B7, B8: Boolean; end;

  

In fact, one byte can represent eight states, because one byte has eight digits.
It is assumed that each bit represents a State. If you want to calculate a combination of the words, energy saving represents 255 cases.
Many "options" in system functions are based on "bit ".

Let's take a few simple exercises about "bit:
{This is a binary function} 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; {Method for assigning a binary number to a variable} procedure tform1.button1click (Sender: tobject ); vaR B1, B2, B3: byte; begin {although Delphi generally does not support binary constants, its Embedded Assembly supports} ASM mov B1, 11100110b end; B2: = 230; B3: = $ E6; {the same value is assigned to B1 B2 B3 now} showmessagefmt ('% d, % d, % d', [B1, B2, B3]); // 230,230,230 end; {use or to ensure that the second and third digits are 1} procedure tform1.button2click (Sender: tobject); var B1, B2: byte; begin ASM mov B1, 11100010b mov B2, 00000110b end; B1: = B1 or B2; showmessage (Tobin (@ B1, 1); {11100110} end; {use and to ensure that the second and third places are 0} procedure tform1.button3click (Sender: tobject); var B1, B2: byte; begin ASM mov B1, 11100010b mov B2, 00000110b end; b1: = B1 and B2; showmessage (Tobin (@ B1, 1); {00000010} end; {use and to detect whether the second and third digits are 1} procedure tform1.button4click (Sender: tobject); var B: byte; begin ASM mov B, 11100010b end; if 2 and B = 2 then showmessage ('second bit is 1'); {the above can be understood: if 00000010b and B = 00000010b then ...} if 4 and B = 4 then showmessage ('third digit is 1'); {it can be understood as: If 00000100b and B = 00000100b then ...} end; {use shr to detect if the second and third digits are 1} procedure tform1.button5click (Sender: tobject); var B: byte; begin ASM mov B, 11100010b end; if odd (B SHR 1) then showmessage ('second digit is 1'); if odd (B SHR 2) then showmessage ('third digit is 1'); end;
 
   
 

An instance that saves eight states with a byte value:

Preparations:
1. Add checklistbox1 to the blank form;
2. Add four buttons and activate the onclick event of button1;
3. Activate the oncreate event of the form.
VaR B: byte; {use it to record the selection status of eight checkboxes} 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 B: = 0; for I: = 0 to checklistbox1.count-1 do case tbutton (sender ). tag of 1: If checklistbox1.checked [I] Then B: = B or (1 shl I); 2: checklistbox1.checked [I]: = odd (B shr I); 3: checklistbox1.checked [I]: = true; 4: checklistbox1.checked [I]: = false; end;

  

Test ("recovery" is written as "reply ):


"Bit" operations are a little abstract. It is much more concise to replace the above operations with "set". Next we will discuss the essence of "set.

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.