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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值