简体   繁体   English

如何在C ++中打印出二进制的unsigned int?

[英]How do I print out an unsigned int in binary in C++?

I am trying to print out an unsigned value in binary in C++. 我试图在C ++中打印出二进制的无符号值。 I have found many hacks for this task such as 我发现很多黑客都可以完成这项任务,例如

http://www.java2s.com/Tutorial/Cpp/0040__Data-Types/Printinganunsignedintegerinbits.htm http://www.java2s.com/Tutorial/Cpp/0040__Data-Types/Printinganunsignedintegerinbits.htm

However, I feel that there should be a much more straightforward way, perhaps with sprintf . 但是,我觉得应该有一个更直接的方式,也许是sprintf After all, there are very straightforward ways to print a value in hex or octal. 毕竟,有十分简单的方法以十六进制或八进制打印值。

Simple - Use STL bitset : 简单 - 使用STL bitset

eg 例如

bitset<10> n (120ul); // 10 bits in this case
cout << n.to_string() << endl;

printf familiy does not support base-2 printing. printf familiy不支持base-2打印。 You need to use a custom or non-standard function, such as itoa (just set the base/radix to 2). 您需要使用自定义或非标准函数,例如itoa (只需将基数/基数设置为2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM