张志峰的博客

水滴石川,积少成多。

导航

delphi 按位运算 not and or xor shl shr

Posted on 2016-05-25 10:58  ╰★张志峰★╮  阅读(3007)  评论(0编辑  收藏  举报

delphi 按位运算 not and or xor shl shr

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Shape1: TShape;
    Label1: TLabel;
    Label2: TLabel;
    Button7: TButton;
    Button8: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  a: Word;
  c: Integer;
begin
  a := 6;   //0000 0000 0000 0000    0000 0000 0000 0110
  c := 12;  //0000 0000 0000 0000    0000 0000 0000 1100     四字节 32 位
  ShowMessage(IntToStr( a and c));
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
  a, b: Word;
//  a, b: Integer;
begin
  a := 6;
  b := 12;
  ShowMessage(IntToStr(a and b));
 
//无符号:
// byte :     一个字节  8位    2的8次方          0-255 (2的8次方-1)
// word :     两个字节 16位    2的16次方         0-256*256 (65535)
// longword:  四个字节 32      2的32次方         0-65536*65536 (4294967295)
//
//有符号:(拿出一位做符号位,表示正负)
//shortint   一个字节 8位     2的8次方          -127-127 (256/2)
//smallint   两个字节 16位    2的16次方         -32767-32767 (256*256/2)
//longint(Ingetger) 四个字节32位  2的32次方  -2147483647-2147483647 (4294967295/2)
end;
 
//not
//1 -> 0 , 0 -> 1
procedure TForm1.Button3Click(Sender: TObject);
var
  a: Word;
begin
  a := 14;    // 0000 0000 0000 1110
  ShowMessage(IntToStr(not a));   //65521  not-> 1111 1111 1111 0001
end;
 
//and
//都是1才1
procedure TForm1.Button4Click(Sender: TObject);
var
  a, b: Word;
begin
  a := 14;    // 0000 0000 0000 1110
  b := 23;    // 0000 0000 0001 0111
  ShowMessage(IntToStr(a and b));//6  and->0000 0000 0000  0110
end;
 
//or
//位 有1则1
procedure TForm1.Button5Click(Sender: TObject);
var
  a, b: Word;
begin
  a := 14;    // 0000 0000 0000 1110
  b := 23;    // 0000 0000 0001 0111
  ShowMessage(IntToStr(a or b));//31  and->0000 0000 0001  1111
end;
 
//xor
//位不相同1
procedure TForm1.Button6Click(Sender: TObject);
var
  a, b: Word;
begin
  a := 14;    // 0000 0000 0000 1110
  b := 23;    // 0000 0000 0001 0111
  ShowMessage(IntToStr(a xor b));//25  and->0000 0000 0001  1001
end;
 
//shl
//说明:左移 右边补0 (超出忽略)
procedure TForm1.Button7Click(Sender: TObject);
var
  a: Word;
  b: Byte;
begin
  a := 14;    // 0000 0000 0000 1110
  ShowMessage(IntToStr(a shl 1));//28  and->0000 0000 0001  1100
  ShowMessage(IntToStr(a shl 3));//112  and->0000 0000 0111  0000
 
  b :=12;     // 0000 1100;
  ShowMessage(IntToStr(b shl 4));   //192     1100 0000
  ShowMessage(IntToStr(b shl 5));   //384    11000 0000
                        //   6      //     11 0000 0000       (超出忽略)
 
end;
 
//shr
//说明:右移  左边补0 (超出忽略)
procedure TForm1.Button8Click(Sender: TObject);
var
  a: Word;
begin
  a := 14;    // 0000 0000 0000 1110
  ShowMessage(IntToStr(a shr 1));//7  shr->0000 0000 0000  0111
  ShowMessage(IntToStr(a shr 2));//3  shr->0000 0000 0000  0011
                           //3             0000 0000 0000  0001
                           //4             0000 0000 0000  0000      (超出忽略)
end;
 
end.