Delphi 的按位运算详解


本文整理自网络,侵删。

 Delphi 的按位运算符共有六个: not and or xor shr shl; 
其中的 not and or xor 也叫逻辑运算符, 其实功能都是一样的, 因为不管什么数据追到底都是 0 和 1 的组合;
在 Delphi 内嵌汇编中, 应该也没什么区别(内嵌汇编还在学习中, 不太熟).
unit Unit1;

interface

uses
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 Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

const
w1: Word = 61680; {二进制表示: 11110000 11110000}
w2: Word = 3855; {二进制表示: 00001111 00001111}
var
w: Word;

{not 运算, 只有一个运算数}
procedure TForm1.Button1Click(Sender: TObject);
begin
w := not w1;

{not 就是按位(给二进制的每一位)取反}
{11110000 11110000 取反后就是:}
{00001111 00001111 }
ShowMessage(IntToStr(w)); {3855}
end;

{and 运算, 需要两个运算数}
procedure TForm1.Button2Click(Sender: TObject);
begin
w := w1 and w2;

{and 就是把两个运算数按位对比, 同是1返回1, 反之返回0}
{w1: 11110000 11110000 与}
{w2: 00001111 00001111 每一位都不同, 所以返回:}
{w : 00000000 00000000}
ShowMessage(IntToStr(w)); {0}
end;

{or 运算, 需要两个运算数}
procedure TForm1.Button3Click(Sender: TObject);
begin
w := w1 or w2;

{and 就是把两个运算数按位对比, 只有其中一个是1就返回1; 都是0才返回0}
{w1: 11110000 11110000 与}
{w2: 00001111 00001111 or 后会返回:}
{w : 11111111 11111111}
ShowMessage(IntToStr(w)); {65535}
end;

{xor 运算, 需要两个运算数}
procedure TForm1.Button4Click(Sender: TObject);
begin
w := w1 or w2;

{and 就是把两个运算数按位对比, 只有两个不一样才返回1; 一样(都是0或都是1)则返回0}
{w1: 11110000 11110000 与}
{w2: 00001111 00001111 xor 后会返回:}
{w : 11111111 11111111}
ShowMessage(IntToStr(w)); {65535; 两个例数不太好, 没给 xor 和 or 区别明显}
end;

{shr 运算, 只有一个运算数}
procedure TForm1.Button5Click(Sender: TObject);
begin
w := w1 shr 1;

{shr 是按位右移, shr 1 是右移一位}
{w1: 11110000 11110000 右移一位后是:}
{w : *1111000 01111000 前面的*就是0了}
ShowMessage(IntToStr(w)); {30840}

{同理, 可以移动几位, 譬如 3 位}
w := w1 shr 3;
ShowMessage(IntToStr(w)); {7710}

{w1 shr 3 相当与 w1 div 2的3次方}
w := w1 div 8;
ShowMessage(IntToStr(w)); {7710}
end;

{shl 运算, 只有一个运算数}
procedure TForm1.Button6Click(Sender: TObject);
var
i: Integer;
begin
w := w1 shl 1;

{shr 是按位左移}
{w1: 11110000 11110000 左移一位后是:}
{w : 1110000 111100000 }
ShowMessage(IntToStr(w)); {57824}

{左移 3 位}
w := w1 shl 3;
ShowMessage(IntToStr(w)); {34688}

{w1 shl 3 相当与 w1 * 2的3次方}
w := w1 * 8;
ShowMessage(IntToStr(w)); {34688}

{注意这里有个问题: w1*8 以后怎么小了呢?}
{因为前面已经定义了 w 是 Word 类型的, 它的大小只有2个字节(二进制16位), 超出会忽略}

{如果换成32位(4字节)的 Integer 类型, 肯定就会有真实的结果:}

i := w1 shl 3;
ShowMessage(IntToStr(i)); {493440}

i := w1 * 8;
ShowMessage(IntToStr(i)); {493440}
end;

end.

相关阅读 >>

Delphi firedac 下的 sqlite [12] - 备忘录

Delphi 中的文件系统进行unicode标准化

Delphi获取进程快照(snapshot)

Delphi 提升Delphi 启动加载速度

Delphi 7 中的随机函数

Delphi xe 使用savestate保存firemonkey状态的示例

Delphi xe2 idhttp 获取utf-8编码中文网页

Delphi打开关闭光驱

Delphi实现自我删除

Delphi tfdmemtable 更新到数据库

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...