什么是一个布尔值在C#中的二进制重新presentation [英] What is the binary representation of a boolean value in c#
问题描述
我知道一个布尔值是1字节(8位)
不过,我想知道是什么,是它的二进制重新presentation。
例如
十进制=>二进制
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 => ???
推荐答案
布尔
是一个内置在C#中的基本类型。任何基础重新presentation将是一个实现细节。
bool
is a built-in basic type in C#. Any underlying representation would be an implementation detail.
的 C#4.0语言规范的第4.1.8规定:
The C# 4.0 Language Specification states in section 4.1.8:
的布尔
键入重presents布尔逻辑量。键入布尔
的可能值是真正
和假
。
The
bool
type represents boolean logical quantities. The possible values of typebool
aretrue
andfalse
.
布尔
和其他类型之间不存在标准转换。尤其是,布尔
类型与整型截然不同的和独立的,而布尔
值不能就地使用的积分值,并且反之亦然。
No standard conversions exist between bool
and other types. In particular, the bool
type is distinct and separate from the integral types, and a bool
value cannot be used in place of an integral value, and vice versa.
在C和C ++语言中,零整数或浮点值或空指针可以转换为布尔值假
和非零整数或浮点数值,或非空指针可以转换为布尔值真正
。在C#中,这种转换是通过显式地将整数或浮点值与零进行比较,或者比较明确的对象引用为空来实现的。
In the C and C++ languages, a zero integral or floating-point value, or a null pointer can be converted to the boolean value false
, and a non-zero integral or floating-point value, or a non-null pointer can be converted to the boolean value true
. In C#, such conversions are accomplished by explicitly comparing an integral or floating-point value to zero, or by explicitly comparing an object reference to null.
如果我们采取这种更深层次的原因看相应的类型是如何在specied中的通用中间语言的(CIL),我们将看到一个CLI布尔类型在内存占用1个字节。在通用语言基础结构的(CLI)规范说,在分区第三,部分1.1.2:
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:
一个CLI布尔类型在存储器中占用1个字节。全零的位模式表示false值。一点点
设置(类似于一个非零整数)中任一个或多个比特图案表示真值
A CLI Boolean type occupies 1 byte in memory. A bit pattern of all zeroes denotes a value of false. A bit pattern with any one or more bits set (analogous to a non-zero integer) denotes a value of true.
不过,这在另一个层面上规定和C#中你不应该去关心;即使CLI规范的未来版本中可能会改变布尔类型的再presentation,或者C#编译器决定在C#中映射布尔
到不同的东西,你的C#code仍然具有相同的语义。
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.
这篇关于什么是一个布尔值在C#中的二进制重新presentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!