Detailed explanation of Delphi's bitwise operations-reply to the "Beginner's assembly" question

Source: Internet
Author: User
Source:
Http://www.cnblogs.com/del/archive/2008/04/02/1055762.html#1134153
Http://www.cnblogs.com/del/archive/2008/04/02/1055648.html#1134081

Delphi has six bitwise operators: Not and or xor shr shl;
The not and or XOR is also called a logical operator. In fact, the functions are the same, because no matter what data is chased, it is a combination of 0 and 1;
In Delphi embedded assembly, there should be no difference (Embedded Assembly is still learning, not familiar ).

The following method can be used to test the example below: http://www.cnblogs.com/del/archive/2008/03/09/1097845.html

Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; button5: tbutton; button6: tbutton; Procedure processing (Sender: Taobao); Procedure processing (Sender: tobject ); procedure button4click (SE Nder: tobject); Procedure button5click (Sender: tobject); Procedure button6click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} const W1: Word = 61680; {binary representation: 11110000 11110000} W2: Word = 3855; {binary representation: 00001111 00001111} var W: word; {not operation, only one operation count} procedure tform1.button1click (Sender: tobject); begin W: = Not W1; {not is bitwise (to every bit of binary) reverse retrieval} {11110000 11110000 00001111 after reverse retrieval:} {0000111 1} showmessage (inttostr (w); {3855} end; {and operation, requires two operation numbers} procedure tform1.button2click (Sender: tobject); begin W: = W1 and W2; {and is to compare two arithmetic operations by bit. 1 returns 1, and 0} {W1: 11110000 11110000 and} {W2: 00001111 00001111 returns 1, so return:} {W: 00000000 00000000} showmessage (inttostr (w); {0} end; {or operation, two operations are required} procedure tform1.button3click (Sender: tobject ); begin W: = W1 or W2; {and is to compare two arithmetic values by bit. If one of them is 1, 1 is returned. 0 returns 0} {W1: 11110000 11110000 and} {W2: 00001111 00001111 or, then returns :}{ W: 11111111 11111111} showmessage (inttostr (w )); {65535} end; {XOR operation, requires two operation numbers} procedure tform1.button4click (Sender: tobject); begin W: = W1 or W2; {and is to compare the two operation numbers by bit, 1 is returned only when there are two differences. If both are 0 or 1, 0} {W1: 11110000 11110000 and} {W2: 00001111 00001111 XOR are returned, :}{ W: 11111111 11111111} showmessage (inttostr (w); {65535; the number of two instances is not good, and XOR and or Significant difference} end; {SHR operation, only one operation number} procedure tform1.button5click (Sender: tobject); begin W: = W1 SHR 1; {SHR is shifted right by bit, SHR 1 is shifted to one right} {W1: 11110000 11110000 shifted to one right followed by :}{ W: * 1111000 before * 0} showmessage (inttostr (w )); {30840} {Likewise, several digits can be moved, for example, 3} W: = W1 SHR 3; showmessage (inttostr (w )); {7710} {W1 SHR 3 is equivalent to the 3rd power of W1 Div 2} W: = W1 Div 8; showmessage (inttostr (w); {7710} end; {SHL operation, only one operation count} procedure tform1. Button6click (Sender: tobject); var I: integer; begin W: = W1 SHL 1; {SHR is shifted left by bit} {W1: 11110000 11110000 shifted left by one after :} {W: 1110000 111100000} showmessage (inttostr (w); {57824} {shifts three places left} W: = W1 SHL 3; showmessage (inttostr (w )); {34688} {W1 SHL 3 is equivalent to the 3rd power of W1 * 2} W: = W1 * 8; showmessage (inttostr (w )); {34688} {Note the following question: why is it small after W1 * 8 ?} {Because the previously defined W is of the Word type, its size is only 2 bytes (16 bits in binary) and will be ignored if it is exceeded} {if it is changed to 32 bits (4 bytes) the actual result is:} I: = W1 SHL 3; showmessage (inttostr (I); {493440} I: = W1 * 8; showmessage (inttostr (I); {493440} end; end.
 
  
 

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.