加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net – 将布尔值转换为整数会返回-1,表示true?

发布时间:2020-12-16 22:35:34 所属栏目:大数据 来源:网络整理
导读:我正在使用一些 VB.NET代码,似乎是使用CInt(myBoolean)将一个布尔值转换为整数。发生的奇怪的事情是,如果值为true,则返回-1。例如: CInt(True) // returns -1CInt(False) // returns 0 这是其他语言中常见的吗? 我认为一个布尔值将为1如果为真,如果为
我正在使用一些 VB.NET代码,似乎是使用CInt(myBoolean)将一个布尔值转换为整数。发生的奇怪的事情是,如果值为true,则返回-1。例如:
CInt(True)  // returns -1
CInt(False) // returns 0

这是其他语言中常见的吗?

我认为一个布尔值将为1如果为真,如果为假则为0。另外,有没有办法让Visual Basic将1赋值为true而不是赋值-1?

通常,值false表示为0,值true表示任何非0整数值。 true和false的具体值(其他)是你不应该依赖的 – 它们可能是实现特定的。我不知道你想要做什么,但它可能是最好不要依赖于True或False具有任何特定的整数值,除非你绝对必须。

最好的解释,我可以找到VB的具体行为来自Wikipedia:

Boolean constant True has numeric value ?1. This is because the Boolean data type is stored as a 16-bit signed integer. In this construct ?1 evaluates to 16 binary 1s (the Boolean value True),and 0 as 16 0s (the Boolean value False). This is apparent when performing a Not operation on a 16 bit signed integer value 0 which will return the integer value ?1,in other words True = Not False. This inherent functionality becomes especially useful when performing logical operations on the individual bits of an integer such as And,Or,Xor and Not.[4] This definition of True is also consistent with BASIC since the early 1970s Microsoft BASIC implementation and is also related to the characteristics of CPU instructions at the time.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读