辩认真假

linzhengqun 2006-12-27 11:39:20
--------------------------------------------------
var
I1, I2: Integer;
b: Boolean;
wb: WordBool;
lb: LongBool;
begin
I1 := $FF00;
b := I1 <> 0; //b = ?
b := Boolean(I1); //b = ?
b := WordBool(I1); //b = ?
b := LongBool(I1); //b = ?

I2 := $FFFF0000;
lb := LongBool(I2); //lb = ?
wb := WordBool(I2); //wb = ?
wb := WordBool(lb); //wb = ?
end;
原因:

结论:

---------------------------------------------------

请填写上面问号的值,并写出原因和结论,答对有奖哦!!!
...全文
265 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
chijingde 2006-12-29
  • 打赏
  • 举报
回复
2、布尔型与布尔型之间的转换是安全的,不管它们之间的大小,编译器保证转换后布尔值相同。
3、不同大小的布尔型之间的转换,可能会使其数值改变


2,3自相矛盾?
yyjzsl 2006-12-28
  • 打赏
  • 举报
回复
这年头200分得帖子不多了

顶一下
w95927w 2006-12-28
  • 打赏
  • 举报
回复
顶! 哇哈哈,给多少分
linzhengqun 2006-12-28
  • 打赏
  • 举报
回复
哈哈,我的结论是这样的:

1、布尔型与整型之间的转换是不可能靠的,你必须保证两者所占字节一样,才能确保无误。
2、布尔型与布尔型之间的转换是安全的,不管它们之间的大小,编译器保证转换后布尔值相同。
3、不同大小的布尔型之间的转换,可能会使其数值改变
ly_liuyang 2006-12-28
  • 打赏
  • 举报
回复
接分来的
real_name 2006-12-27
  • 打赏
  • 举报
回复
up
chijingde 2006-12-27
  • 打赏
  • 举报
回复
...

我使用布尔值只用Boolean

其他一概不用...

估计没啥帮助啦
chijingde 2006-12-27
  • 打赏
  • 举报
回复
1字节 Boolean,ByteBool
2字节 WordBool
4字节 BOOL,LongBool
linzhengqun 2006-12-27
  • 打赏
  • 举报
回复
认真填写上面的问题,可能对你以后使用布尔值大有帮助哦。

楼上贴帮助出来作什么呢?
chijingde 2006-12-27
  • 打赏
  • 举报
回复
The four predefined Boolean types are Boolean, ByteBool, WordBool, and LongBool. Boolean is the preferred type. The others exist to provide compatibility with other languages and operating system libraries.

A Boolean variable occupies one byte of memory, a ByteBool variable also occupies one byte, a WordBool variable occupies two bytes (one word), and a LongBool variable occupies four bytes (two words).

Boolean values are denoted by the predefined constants True and False. The following relationships hold.

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
A value of type ByteBool, LongBool, or WordBool is considered True when its ordinality is nonzero. If such a value appears in a context where a Boolean is expected, the compiler automatically converts any value of nonzero ordinality to True.
The previous remarks refer to the ordinality of Boolean values, not to the values themselves. In Delphi, Boolean expressions cannot be equated with integers or reals. Hence, if X is an integer variable, the statement

if X then ...;

generates a compilation error. Casting the variable to a Boolean type is unreliable, but each of the following alternatives will work.

if X <> 0 then ...; { use longer expression that returns Boolean value }
var OK: Boolean { use Boolean variable }
...
if X <> 0 then OK := True;
if OK then ...;
zuoansuifeng 2006-12-27
  • 打赏
  • 举报
回复
。。。接分
madyak 2006-12-27
  • 打赏
  • 举报
回复
UP一下
YouTuBe 2006-12-27
  • 打赏
  • 举报
回复
帮顶:)
fim 2006-12-27
  • 打赏
  • 举报
回复
看得晕了,谢楼主,蹭点水分:)
linzhengqun 2006-12-27
  • 打赏
  • 举报
回复
所以,如果我们用SendMessage的时候,如果返回值是BOOL值,则要特别小心了。最好用BOOL去强制转换,如果用Boolean,则非常不安全。
linzhengqun 2006-12-27
  • 打赏
  • 举报
回复
zaza_bbface: 100分
chijingde(AD): 顶得这么辛苦:20分 ^_~
zaza_bbface 2006-12-27
  • 打赏
  • 举报
回复
var
I1, I2: Integer;
b: Boolean;
wb: WordBool;
lb: LongBool;
begin
I1 := $FF00;
b := I1 <> 0; //b = true
b := Boolean(I1); //b = false
b := WordBool(I1); //b = true
b := LongBool(I1); //b = true

I2 := $FFFF0000;
lb := LongBool(I2); //lb = true
wb := WordBool(I2); //wb = false
wb := WordBool(lb); //wb = true
end;

在电脑上试了一下。
我这么理解:
1.强制类型转换,字节数不等,将截断高字节
如:
 I1 := $FF00;
b := Boolean(I1); //b = false
截断后Boolean(I1);值为$00;

I2 := $FFFF0000;
 wb := WordBool(I2); //wb = false
截断后WordBool(I2); 值为$0000;
2.布尔型的值
I2 := $FFFF0000;
lb := LongBool(I2);
用 showmessage(inttostr(integer(lb)));可以看到其值仍为$FFFF0000;
3.字节数不同的布尔型之间的强制转换
应该是为了保证语意逻辑上的一致,换句话说,一个true值的布尔型,不管你转成哪种布尔型,它都应该是true。而按上面两条,将有可能破坏这个逻辑。因此,在转换后采用了特定的值,即0,1或0,-1
0,1用于boolean型
0,-1用于WordBool及LongBool
因此,
I2 := $FFFF0000;
lb := LongBool(I2);
wb := WordBool(lb); //LongBool强制转换成WordBool,其值为-1
chijingde 2006-12-27
  • 打赏
  • 举报
回复
这年头200分得帖子不多了

再顶一下
chijingde 2006-12-27
  • 打赏
  • 举报
回复
厄...

怎么填写?闭卷还是开卷?

闭卷的话答不上来...我脑子犯糊涂,肯定会算错

开卷的话...懒得打开delphi...哈哈

应该是由于字节数不同的类型相互转换,导致了结果变化

本来想兑奖的...

关注一下

期待答案
caixia615 2006-12-27
  • 打赏
  • 举报
回复
关注
加载更多回复(5)

828

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧