的std ::二进制(n)的 [英] std::binary(n)

查看:72
本文介绍了的std ::二进制(n)的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我刚刚放在一起的东西;虽然我不太了解

使这个行为与std :: hex和std :: dec一样,这对我来说非常好,但我想我可以改进这个。

建议和火焰都欢迎。要使用,试试这个:


unsigned char ch = 0x80;

std :: cout<< std :: binary(ch)<< " \ n";


所以,这里的代码如下:


命名空间std

{

模板< typename _T>

struct _binary {std :: string _digits; };


模板< typename T>

inline _binary< T>二元(T n)

{

_binary< T> __binary;

int bits = sizeof(n);


开关(位)

{

情况1:位= 7;休息;

案例2:比特= 15;休息;

案例4:比特= 31;休息;

//必要时加上更多

}


for(int i = bits; i> = 0; i--)

{

if((n>> i)& 1)

__binary._digits.append(" ; 1");

其他

__binary._digits.append(" 0");

}

返回__binary;

}


模板< typename T>

ostream& operator<(&ostream& stream,_binary< T> __binary)

{

stream<< __binary._digits;

返回流;

}

}

-
http://www.munted.org.uk


小睡一下,它拯救了生命。

Here''s something I put together just now; whilst I don''t know enough
to make this act the same way std::hex and std::dec does, this works
quite well enough for me, but I think I could improve on this.
Suggestions and flames both welcome. To use, try this:

unsigned char ch = 0x80;
std::cout << std::binary(ch) << "\n";

So, here''s the code as follows:

namespace std
{
template <typename _T>
struct _binary { std::string _digits; };

template <typename T>
inline _binary<T> binary(T n)
{
_binary<T> __binary;
int bits = sizeof(n);

switch (bits)
{
case 1 : bits = 7; break;
case 2 : bits = 15; break;
case 4 : bits = 31; break;
// add more if necessary
}

for (int i = bits; i >= 0; i--)
{
if ((n >> i) & 1)
__binary._digits.append("1");
else
__binary._digits.append("0");
}

return __binary;
}

template <typename T>
ostream& operator<<(ostream& stream, _binary<T> __binary)
{
stream << __binary._digits;
return stream;
}
}
--
http://www.munted.org.uk

Take a nap, it saves lives.

推荐答案

Alex Buell写道:
Alex Buell wrote:
这是我放的东西刚才在一起;虽然我不太了解
使这个行为与std :: hex和std :: dec相同,这对我来说相当不错,但我想我可以改进这个。
建议和火焰都欢迎。要使用,试试这个:

你的意思是,除了使用17.4.3.1下的实现保留的标识符添加到命名空间std和

之外的事实?

unsigned char ch = 0x80;
std :: cout<< std :: binary(ch)<< \ n;

所以,这里的代码如下:

命名空间std
{
模板< typename _T> ;
struct _binary {std :: string _digits;模板< typename T>
inline _binary< T>二进制(T n)
{binary< T> __binary;
int bits = sizeof(n);

switch(bits)
{
案例1:bits = 7;休息;
案例2:比特= 15;休息;
案例4:比特= 31;休息;
//如果有必要添加更多


for(int i = bits; i> = 0; i--)
{
if((n>> i)& 1)
__binary._digits.append(" 1");

__binary._digits.append(" 0" );


返回__binary;
}

模板< typename T>
ostream&运算符<<(ostream& stream,_binary< T> __binary)
{<<< __binary._digits;
返回流;
}
}
Here''s something I put together just now; whilst I don''t know enough
to make this act the same way std::hex and std::dec does, this works
quite well enough for me, but I think I could improve on this.
Suggestions and flames both welcome. To use, try this:
You mean, other than the fact that you''re adding to namespace std and
using identifiers reserved to the implementation under 17.4.3.1?
unsigned char ch = 0x80;
std::cout << std::binary(ch) << "\n";

So, here''s the code as follows:

namespace std
{
template <typename _T>
struct _binary { std::string _digits; };

template <typename T>
inline _binary<T> binary(T n)
{
_binary<T> __binary;
int bits = sizeof(n);

switch (bits)
{
case 1 : bits = 7; break;
case 2 : bits = 15; break;
case 4 : bits = 31; break;
// add more if necessary
}

for (int i = bits; i >= 0; i--)
{
if ((n >> i) & 1)
__binary._digits.append("1");
else
__binary._digits.append("0");
}

return __binary;
}

template <typename T>
ostream& operator<<(ostream& stream, _binary<T> __binary)
{
stream << __binary._digits;
return stream;
}
}



2006年5月30日星期二格林尼治标准时间23:11:18,我挥了挥棒,这条消息神奇地出现了:
On Tue, 30 May 2006 23:11:18 GMT, I waved a wand and this message
magically appeared:
这里有什么东西我刚刚放在一起;虽然我不太了解
使这个行为与std :: hex和std :: dec相同,这对我来说相当不错,但我想我可以改进这个。
建议和火焰都欢迎。要使用,试试这个:
Here''s something I put together just now; whilst I don''t know enough
to make this act the same way std::hex and std::dec does, this works
quite well enough for me, but I think I could improve on this.
Suggestions and flames both welcome. To use, try this:


你的意思是,除了使用17.4.3.1下的实现保留的标识符添加到命名空间std和
之外?


You mean, other than the fact that you''re adding to namespace std and
using identifiers reserved to the implementation under 17.4.3.1?




好​​点,我想我应该读一读17.4.3.1。

-
http://www.munted.org.uk


小睡一下,它可以节省生活。



Good point, guess I should read up on 17.4.3.1.
--
http://www.munted.org.uk

Take a nap, it saves lives.


2006年5月30日星期二,格林尼治标准时间23:11:18,我挥了挥棒,这条消息

神奇地出现了:
On Tue, 30 May 2006 23:11:18 GMT, I waved a wand and this message
magically appeared:
这是我刚刚放在一起的东西;虽然我不太了解
使这个行为与std :: hex和std :: dec相同,这对我来说相当不错,但我想我可以改进这个。
建议和火焰都欢迎。要使用,试试这个:
Here''s something I put together just now; whilst I don''t know enough
to make this act the same way std::hex and std::dec does, this works
quite well enough for me, but I think I could improve on this.
Suggestions and flames both welcome. To use, try this:


你的意思是,除了使用17.4.3.1下的实现保留的标识符添加到命名空间std和
之外?


You mean, other than the fact that you''re adding to namespace std and
using identifiers reserved to the implementation under 17.4.3.1?




嗯,哪本书和哪个版本会是什么?如果它是C ++

编程语言书,17.4.3只涉及集合。

-
http://www.munted.org.uk


小睡一下,它拯救了生命。



Umm, in which book and which edition would that be? If it''s the C++
Programming Language book, 17.4.3 only relates to sets.
--
http://www.munted.org.uk

Take a nap, it saves lives.


这篇关于的std ::二进制(n)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆