显示对象中的位
我正在读一本关于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该查看 System::Convert::ToString
输出:当前状态:1011
编辑:如果您想要空零“填充”,只需执行以下操作:
如果您希望将其分割成字节大小的片段,则只需分割结果并插入空格/连字符。
You should look into System::Convert::ToString
Output: Current Status: 1011
Edit: if you want the empty zero 'padding' just do:
If you want it segmented into byte size pieces, then just split up the result and insert space / hyphens.
第一件事是将值转换为整数。我不确定在 C++/CLI 中执行此操作的最佳方法,但在 C 中,它是
(int)status
。C++ 不提供以二进制显示值的方法,但它允许显示十六进制。对此的声明如下:
输出应为
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:
The output should be
0000000b
.首先,作者知道,因为
status
正在与三个枚举值进行 OR 运算,只需将这三个值加在一起,您就会得到作者所说的
1011
(您可以截断所有前导零)。如果您现在还没有遇到过按位运算:管道|
用于对值执行按位或运算。您只需将1
的所有数字相加即可,如下所示:第二:就像我之前的发帖人 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 valuesJust 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 are1
like this: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 thestd::cout
in C++ are able to. First thought would be to use theBitConverter
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