C# 中布尔值的二进制表示是什么
我知道布尔值是 1 个字节(8 位长) 但我想知道它的二进制表示是什么。 例如 小数=>二进制 4=> 100 (0000 0100) 8=> 1000 (0000 1000) 布尔值=> ???
I know that a boolean value is 1 byte (8 bits long)
But I would like to know is what is its binary representation.
e.g.
decimal => binary
4 => 100 (0000 0100)
8 => 1000 (0000 1000)
bool value => ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
bool
是 C# 中内置的基本类型。任何底层表示都将是实现细节。C# 4.0 语言规范在第 4.1.8 节中指出:
如果我们更深入地了解这一点,看看如何在通用中间语言 (CIL) 中指定相应的类型,我们将看到 CLI 布尔类型在内存中占用 1 个字节。通用语言基础设施 (CLI) 规范在第 III 部分第 1.1.2 节中表示:
然而,这是在另一个级别上指定的,在 C# 中您不必关心;即使 CLI 规范的未来版本可能会更改布尔类型的表示,或者 C# 编译器决定将 C# 中的
bool
映射到不同的内容,您的 C# 代码仍将具有相同的语义。bool
is a built-in basic type in C#. Any underlying representation would be an implementation detail.The C# 4.0 Language Specification states in section 4.1.8:
If we take this one level deeper and see how the corresponding type is specied in the Common Intermediate language (CIL) we will see that a CLI Boolean type occupies 1 byte in memory. The Common Language Infrastructure (CLI) specification says in Partition III, section 1.1.2:
However, this is specified on another level and from within C# you should not have to care; even if a future version of the CLI specification might change the representation of the boolean type, or if the C# compiler decided to map a
bool
in C# to something different, your C# code would still have the same semantics.下面是一段简短的代码,演示了
bool
的底层表示,在当前平台上,无论它运行在何处:(请注意,虽然
1
似乎代表true
,而0
似乎代表false
,这只是一个实现细节,您不应该依赖此细节,或者。假设它将在不同版本和/或实现中保持一致,甚至当前平台始终使用相同的一致表示。)编辑...
ECMA CLI 规范(第 III 部分,第 1.1.2 节)对于
布尔
类型:看来当前的 Microsoft CLR 遵守 ECMA 规范,允许
true
的多种表示形式。以下示例显示单个“False”行(对于0
),后跟 255 行“True”:Here's a quick bit of code that demonstrates the underlying representation of
bool
, on the current platform wherever it happens to be running:(Note that although
1
appears to representtrue
and0
appears to representfalse
, this is just an implementation detail. You shouldn't rely on this detail, or assume that it will remain consistent across different versions and/or implementations, or even that the current platform always uses the same consistent representation.)EDIT...
The ECMA CLI spec (partition III, section 1.1.2) is pretty clear about the allowable representations of the
Boolean
type:It appears that the current Microsoft CLR adheres to the ECMA spec in allowing multiple representations of
true
. The following example displays a single "False" line (for0
) followed by 255 lines of "True":我并不矛盾 0xA3 的答案,但如果您使用:
您将得到
{ 1 }
和{ 0 }
的字节数组。换句话说,二进制值将是00000001
和00000000
。这并不意味着 .NET 就是这样处理内存中的布尔值的 - 它只是将它们转换为字节数组的方式。
I'm not contradicting 0xA3's answer, but if you use:
You'll get a byte array of
{ 1 }
and{ 0 }
. In other words, the binary values would be00000001
and00000000
.This doesn't mean that's how .NET handles booleans in memory - it's just how it converts them to byte arrays.
几乎所有语言/环境(不仅是 .NET)都将
true
实现为等于整数值 1,将false
实现为等于 0。1)然而,有一个重要的例外,即 VB6,其
true
等于 –1。这给迁移到 .NET 带来了很多困难,因为 VB6 的松散类型系统允许在同一表达式中混合整数和布尔值,而2 And True
在 VB6 中的含义与在 VB6 中的含义不同。 VB.NET。1) 尽管许多系统允许在布尔上下文中将任何不等于 0 的数值隐式转换为
true
。一些(特别是动态)语言甚至更进一步,说除了特殊对象(例如None
、空数组、列表继续......)之外的所有对象都等于true
。Almost all languages/environments (not only .NET) implement
true
as equivalent to the integral value 1, andfalse
equal to 0.1)However, there’s one important exception, namely VB6, which had
true
equal to –1. This made quite a few things difficult for the migration to .NET, because the loose type system of VB6 allowed to mix integers and booleans in the same expression, and2 And True
meant something else in VB6 than in VB.NET.1) Although many systems allow an implicit conversion of any numeric value unequal to 0 to
true
in a boolean context. Some (especially dynamic) languages even go further, and say that all objects except for special objects (e.g.None
, empty array, the list goes on …) equaltrue
.通常,布尔值由全零表示的“假”和其他值表示的“真”。为简单起见,由于 Two 的补码<,通常为 -1(有符号整数类型的所有位) /a>.
Typically
Boolean
values are represented byfalse
being all zeros andtrue
being anything else. For simplicity this is normally -1 (all bits on of a signed integral type) due to Two's Complement.