C# 中布尔值的二进制表示是什么

发布于 2024-09-11 06:50:43 字数 118 浏览 8 评论 0原文

我知道布尔值是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

哭泣的笑容 2024-09-18 06:50:43

bool 是 C# 中内置的基本类型。任何底层表示都将是实现细节。

C# 4.0 语言规范在第 4.1.8 节中指出:

bool 类型表示布尔逻辑量。 bool 类型的可能值为 truefalse

bool 和其他类型之间不存在标准转换。特别是,bool 类型与整型不同且独立,并且 bool 值不能用来代替整型值,反之亦然。

在 C 和 C++ 语言中,零整数或浮点值或空指针可以转换为布尔值 false,以及非零整数或浮点值,或者非空指针可以转换为布尔值true。在 C# 中,此类转换是通过显式将整数或浮点值与零进行比较,或者通过显式将对象引用与 null 进行比较来完成的。

如果我们更深入地了解这一点,看看如何在通用中间语言 (CIL) 中指定相应的类型,我们将看到 CLI 布尔类型在内存中占用 1 个字节。通用语言基础设施 (CLI) 规范在第 III 部分第 1.1.2 节中表示:

CLI 布尔类型在内存中占用 1 个字节。全零的位模式表示假值。一点
具有任何一个或多个位集(类似于非零整数)的模式表示值为 true。

然而,这是在另一个级别上指定的,在 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:

The bool type represents boolean logical quantities. The possible values of type bool are true and false.

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.

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.

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:

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.

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.

孤凫 2024-09-18 06:50:43

下面是一段简短的代码,演示了 bool 的底层表示,在当前平台上,无论它运行在何处:(

var x = new NotAGoodIdea();

x.TheBool = true;
Console.WriteLine(x.TheByte);    // 1

x.TheBool = false;
Console.WriteLine(x.TheByte);    // 0

// ...

[StructLayout(LayoutKind.Explicit)]
public struct NotAGoodIdea
{
    [FieldOffset(0)]
    public bool TheBool;
    [FieldOffset(0)]
    public byte TheByte;
}

请注意,虽然 1 似乎代表 true ,而 0 似乎代表 false,这只是一个实现细节,您不应该依赖此细节,或者。假设它将在不同版本和/或实现中保持一致,甚至当前平台始终使用相同的一致表示。)

编辑...

ECMA CLI 规范(第 III 部分,第 1.1.2 节)对于 布尔类型:

1.1.2 布尔数据类型

CLI 布尔类型占用 1 个字节
记忆。全零的位模式
表示 false 值。一点
具有任意一个或多个位集的模式
(类似于非零整数)
表示值为 true。

看来当前的 Microsoft CLR 遵守 ECMA 规范,允许 true 的多种表示形式。以下示例显示单个“False”行(对于 0),后跟 255 行“True”:

// re-use the NotAGoodIdea struct from the previous example
var x = new NotAGoodIdea();

for (int i = 0; i < 256; i++ )
{
    x.TheByte = (byte)i;
    Console.WriteLine(x.TheBool);
}

Here's a quick bit of code that demonstrates the underlying representation of bool, on the current platform wherever it happens to be running:

var x = new NotAGoodIdea();

x.TheBool = true;
Console.WriteLine(x.TheByte);    // 1

x.TheBool = false;
Console.WriteLine(x.TheByte);    // 0

// ...

[StructLayout(LayoutKind.Explicit)]
public struct NotAGoodIdea
{
    [FieldOffset(0)]
    public bool TheBool;
    [FieldOffset(0)]
    public byte TheByte;
}

(Note that although 1 appears to represent true and 0 appears to represent false, 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:

1.1.2 Boolean data type

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.

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 (for 0) followed by 255 lines of "True":

// re-use the NotAGoodIdea struct from the previous example
var x = new NotAGoodIdea();

for (int i = 0; i < 256; i++ )
{
    x.TheByte = (byte)i;
    Console.WriteLine(x.TheBool);
}
小红帽 2024-09-18 06:50:43

我并不矛盾 0xA3 的答案,但如果您使用:

BitConverter.GetBytes(true);
BitConverter.GetBytes(false);

您将得到 { 1 }{ 0 } 的字节数组。换句话说,二进制值将是 0000000100000000

这并不意味着 .NET 就是这样处理内存中的布尔值的 - 它只是将它们转换为字节数组的方式。

I'm not contradicting 0xA3's answer, but if you use:

BitConverter.GetBytes(true);
BitConverter.GetBytes(false);

You'll get a byte array of { 1 } and { 0 }. In other words, the binary values would be 00000001 and 00000000.

This doesn't mean that's how .NET handles booleans in memory - it's just how it converts them to byte arrays.

酒儿 2024-09-18 06:50:43

几乎所有语言/环境(不仅是 .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, and false 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, and 2 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 …) equal true.

心凉 2024-09-18 06:50:43

通常,布尔值由全零表示的“假”和其他值表示的“真”。为简单起见,由于 Two 的补码<,通常为 -1(有符号整数类型的所有位) /a>.

Typically Boolean values are represented by false being all zeros and true being anything else. For simplicity this is normally -1 (all bits on of a signed integral type) due to Two's Complement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文