procedure TForm1.Button1Click(Sender: TObject);
const
track5 = $11000;
var
test:integer;
begin
test:= $11010;
if Boolean( test and track5 )then ShowMessage('23道');
end;与procedure TForm1.Button1Click(Sender: TObject);
const
track5 = $11000;
var
test:integer;
begin
//test:= $11010;
if Boolean( $11010 and track5 )then ShowMessage('23道');
end;两个过程:第一个过程是用变量test代替了$11010,结果if判断为假。
第二个过程是直接用了$11010参与运算,结果if判断为真。搞不懂为啥两个过程的结果会不一样。是不是变量类型错了?附:这个问题可以用if 1 < ( test and track5 )then ShowMessage('23道');
来解决,但是不想更改if表达式,请教各路高手。 谢谢!!!
const
track5 = $11000;
var
test:integer;
begin
test:= $11010;
if Boolean( test and track5 )then ShowMessage('23道');
end;与procedure TForm1.Button1Click(Sender: TObject);
const
track5 = $11000;
var
test:integer;
begin
//test:= $11010;
if Boolean( $11010 and track5 )then ShowMessage('23道');
end;两个过程:第一个过程是用变量test代替了$11010,结果if判断为假。
第二个过程是直接用了$11010参与运算,结果if判断为真。搞不懂为啥两个过程的结果会不一样。是不是变量类型错了?附:这个问题可以用if 1 < ( test and track5 )then ShowMessage('23道');
来解决,但是不想更改if表达式,请教各路高手。 谢谢!!!
最好还是用(test and track5) <> 0第二个过程,Delphi编译期已经计算出 Boolean($11010 and track5) = True,所以
if Boolean($11010 and track5) then
ShowMessage('23道');
实际上是
ShowMessage('23道');
if判断被优化掉了
const
track5 = $11000;
var
test:integer;
begin
test:= $11010;
if WordBool( test and track5 ) then
ShowMessage('23道')
else
showmessage('btn1');end;你试试,这种情况是一样的结果。我现在还没弄太明白是怎么回事。
To:hhnick(nick)
由于你的指路,小弟找到了一篇关于Boolean类型的文章,不敢独享,但愿能对你有所帮助。==========================================================================================
Delphi中布尔类型辨析
苏涌(2000-09-29)
Delphi中预定义的布尔类型有四种:Boolean ,ByteBool,WordBool,LongBool。其中,Boolean 类型是首选布尔类型,其余三种是为其它编程语言和Windows 环境提供兼容性支持。这些布尔类型在使用上大同小异,但如果混淆使用将可能会有意外结果。 现做简单辨析供大家参考。 一、从资源占用的角度进行比较
一项Boolean 类型的数据占用 1字节的内存; 一项ByteBool类型的数据占用 1字节的内存; 一项WordBool类型的数据占用 2字节的内存; 一项LongBool类型的数据占用 4字节的内存。 如果开发者在进行程序设计时将构造一种含有布尔数据类型的结构类型,那么在资源占用方面将有所考虑。尽管这些数据类型之间是可以相互赋值的,但某些特殊情况下是有区别的。首先看下面的语句:
type ByteBoolFile = file of ByteBool; LongBoolFile = file of LongBool; 这里,如果在这两中类型文件中存储相同数量的布尔值,其文件大小是不同的。而对同一物理文件按照这两种类型文件分别读取数据,其结果更是相去甚远。 下面是比较ByteBool和LongBool的一段程序,得到的文件 test1.bin和 test2.bin的文件尺寸分别为 4字节和16字节。 procedure CompareByteBoolWithLongBool; const FName1 = 'c: est1.bin'; FName2 = 'c: est2.bin'; type ByteBoolFile = file of ByteBool; LongBoolFile = file of LongBool; var BF: ByteBoolFile; LF: LongBoolFile; B: Boolean; begin B := False; AssignFile(BF, FName1); Rewrite(BF); Write(BF, B, B, B, B); CloseFile(BF); AssignFile(LF, FName2); Rewrite(LF); Write(LF, B, B, B, B); CloseFile(LF); end; 有兴趣的朋友可以在此基础上再比较一下读取数据的区别,你会有更奇特的发现。 二、从布尔值的操作角度进行比较
在Delphi中,布尔值只能被赋予预定义的常量True和 False之一。上述四种布尔数据类型有如下关系: Boolean ByteBool,WordBool,LongBool False < True False <> True Ord(False) = 0 Ord(False) = 0 Ord(True) = 1 Ord(True) <> 0 Succ(False) = True Succ(False) = True Pred(True) = False Pred(False) = True 不难看出,Boolean 类型的有序的,而其它三种布尔数据类型是无序的。下面的程序给出了其中的部分区别: procedure CompareBooleanWithLongBool; var B: Boolean; LB: LongBool; begin B := False; LB := False; if Ord(B) = Ord(LB) then ShowMessage('Ord(B) = Ord(LB) [B = LB = False]') //将被执行 else ShowMessage('Ord(B) <> Ord(LB) [B = LB = False]'); B := True; LB := True; if Ord(B) = Ord(LB) then ShowMessage('Ord(B) = Ord(LB) [B = LB = True]') else ShowMessage('Ord(B) <> Ord(LB) [B = LB = True]'); //将被执行 ShowMessage('Ord(B) = ' + IntToStr(Ord(B))); //一定是 1 ShowMessage('Ord(LB) = ' + IntToStr(Ord(LB))); //可能是-1 end;
==========================================================================================