显示对象中的位

发布于 2024-11-16 03:00:31 字数 517 浏览 2 评论 0原文

我正在读一本关于 C++ 的书。作者显示了这个枚举:

[Flags] enum class FlagBits{ Ready = 1, ReadMode = 2, WriteMode = 4,
EOF = 8, Disabled = 16};
FlagBits status = FlagBits::Ready | FlagBits::ReadMode | FlagBits::EOF;

他说状态等于'0000 0000 0000 0000 0000 0000 0000 1011',但是当我将状态写入控制台时:

Console::WriteLine(L”Current status: {0}”, status);

它显示:'当前状态:Ready,ReadMode,EOF'。他怎么知道它,我如何将状态写入控制台以显示其二进制形式?

I'm reading a book about C++. The author shows this enum:

[Flags] enum class FlagBits{ Ready = 1, ReadMode = 2, WriteMode = 4,
EOF = 8, Disabled = 16};
FlagBits status = FlagBits::Ready | FlagBits::ReadMode | FlagBits::EOF;

and he says that status is equals to '0000 0000 0000 0000 0000 0000 0000 1011', but when I write status to console:

Console::WriteLine(L”Current status: {0}”, status);

it shows: 'Current status: Ready, ReadMode, EOF'. How can he know it, and how can I write status to console to show its binary form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦萦几度 2024-11-23 03:00:31

您应该查看 System::Convert::ToString

int main(array<System::String ^> ^args)
{
    FlagBits status = FlagBits::Ready | FlagBits::ReadMode | FlagBits::EOF;

    Console::WriteLine(L"Current status: {0}", System::Convert::ToString( ( int ) status, 2 ) );
    Console::ReadLine();

    return 0;
}

输出:当前状态:1011

编辑:如果您想要空零“填充”,只需执行以下操作:

Console::WriteLine(L"Current status: {0}", System::Convert::ToString( ( int ) status, 2 )->PadLeft( 32, '0' ) );

如果您希望将其分割成字节大小的片段,则只需分割结果并插入空格/连字符。

You should look into System::Convert::ToString

int main(array<System::String ^> ^args)
{
    FlagBits status = FlagBits::Ready | FlagBits::ReadMode | FlagBits::EOF;

    Console::WriteLine(L"Current status: {0}", System::Convert::ToString( ( int ) status, 2 ) );
    Console::ReadLine();

    return 0;
}

Output: Current Status: 1011

Edit: if you want the empty zero 'padding' just do:

Console::WriteLine(L"Current status: {0}", System::Convert::ToString( ( int ) status, 2 )->PadLeft( 32, '0' ) );

If you want it segmented into byte size pieces, then just split up the result and insert space / hyphens.

对你再特殊 2024-11-23 03:00:31

第一件事是将值转换为整数。我不确定在 C++/CLI 中执行此操作的最佳方法,但在 C 中,它是 (int)status

C++ 不提供以二进制显示值的方法,但它允许显示十六进制。对此的声明如下:

Console::WriteLine(L"Current status: {0:x}", (int)status);

输出应为 0000000b

The first thing will be to cast the value to an integer. I'm not sure of the best way to do this in C++/CLI, but in C it would be (int)status.

C++ does not offer a way to display a value in binary, but it does allow hexadecimal. Here's the statement for that:

Console::WriteLine(L"Current status: {0:x}", (int)status);

The output should be 0000000b.

策马西风 2024-11-23 03:00:31

首先,作者知道,因为 status 正在与三个枚举值进行 OR 运算,

FlagBits::Ready = 1 // Binary 0001
FlagBits::ReadMode = 2 // Binary 0010
FlagBits::EOF = 8 // Binary 1000

只需将这三个值加在一起,您就会得到作者所说的 1011 (您可以截断所有前导零)。如果您现在还没有遇到过按位运算:管道 | 用于对值执行按位或运算。您只需将 1 的所有数字相加即可,如下所示:

 0001
 0010
+1000
-----
=1011

第二:就像我之前的发帖人 Mark Ransom 一样,我实际上不知道 C# 是否能够像“oldschool”那样以二进制形式打印值C 中的 printf() 函数或 C++ 中的 std::cout 都可以。首先想到的是使用 .NET 的 BitConverter 类并自己编写这样的二进制打印函数。

希望有帮助。

编辑:在这里找到了一个使用我提到的 BitConverter 的示例。我没有详细检查它,但乍一看似乎没问题: http://www.eggheadcafe.com/software/aspnet/33292766/print-a-number-in-binary-format.aspx

First, the author knows that because status is being OR'ed with the three enum values

FlagBits::Ready = 1 // Binary 0001
FlagBits::ReadMode = 2 // Binary 0010
FlagBits::EOF = 8 // Binary 1000

Just add these three values together and you'll get the 1011 the author talks about (you can truncate all leading zeroes). If you didn't come accross bitwise operations by now: The pipe | is used to do a bitwise OR-Operation on the values. You just add up all digits that are 1 like this:

 0001
 0010
+1000
-----
=1011

Second: Like my previous poster, Mark Ransom, I don't actually know if C# is capable of printing values in binary form like the "oldschool" printf() function in C or the std::cout in C++ are able to. First thought would be to use the BitConverter class of .NET and writing such a binary-print-function myself.

Hope that helps.

EDIT: Found an example here using the BitConverter I mentioned. I didn't check it in detail, but on first looks it seems alright: http://www.eggheadcafe.com/software/aspnet/33292766/print-a-number-in-binary-format.aspx

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