对象表示的位模式 [英] Bit-Pattern of Representation of Objects

查看:80
本文介绍了对象表示的位模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力想出一种方法来辨别C ++对象表示的实际

位模式(尤其是
小型内置类型的对象) ),我提出了

之后的混乱。但是我想知道,有没有更简单的方法

这样做?这看起来很笨拙。


#include< iostream>

#include< cmath>

template< typename T>

void Binary(T const& object)

{

size_t size = 8 * sizeof(object); //以位为单位的对象大小。

unsigned long long int mask =

static_cast< unsigned long long int>

(pow(2.0, static_cast< double>(size - 1))+ 0.1);

unsigned long long int pattern =

* reinterpret_cast< const unsigned long long int *>(& ();(掩码0;掩码>> = 1)
$

else std :: cout<< 0;

}

std :: cout<< std :: endl;

返回;

}

-

干杯,

Robbie Hatley

美国加利福尼亚州东塔斯廷市

在pac bell dot net上孤独的狼内战

(put" [usenet]" ;受制于绕过垃圾邮件过滤器)
http://home.pacbell.net / earnur /

解决方案

* Robbie Hatley:


我当时努力想出一种方法来辨别C ++对象表示的实际位模式(特别是
小内置类型的对象),我来了一塌糊涂之后的

。但是我想知道,有没有更简单的方法

这样做?这看起来很笨拙。


#include< iostream>

#include< cmath>

template< typename T>

void Binary(T const& object)

{

size_t size = 8 * sizeof(object); //对象的大小(以位为单位)。



使用CHAR_BITS或其他任何名称:一个字节不一定是8位。


unsigned long long int mask =



C ++没有'long long'类型,但是。


static_cast< unsigned long long int>

(pow(2.0,static_cast< double>(size - 1))+ 0.1);



使用左移运算符。


unsigned long long int pattern =

* reinterpret_cast< const unsigned long long int *>(& object);



你不知道sizeopf(object)< = sizeof(long long)。


for(; mask 0; mask>> = 1)

{

if(pattern& mask)std :: cout< < " 1" ;;

else std :: cout<< " 0英寸;



std :: cout<< !!(模式和掩码);


}

std :: cout<< std :: endl;

return;



不必要''返回''。


}



-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


< blockquote> Robbie Hatley发布:


我正在努力想出一种方法来辨别C ++表示的实际

位模式对象



这里有一些我最近写的代码如果你感兴趣的话:


#include< cstddef> ;

#include< cassert>

#include< climits>

#include< ostream>

void PrintBits(void const * const mem,size_t amount_bytes,std :: ostream& os)

{

assert(mem);

断言( amount_bytes);


char static str [CHAR_BIT + 1] = {};


unsigned char const * p = reinterpret_cast< unsigned char const *>(mem);


do

{

unsigned const byte_val = * p ++;


char * pos = str;


unsigned to_and_with = 1U<< CHAR_BIT - 1;


do * pos ++ = byte_val& to_and_with? ''1'':''0'';

while(to_and_with>> = 1);


os<< str;


} while(--amount_bytes);

}

模板< class T>

inline void PrintObjBits(T const& obj,std :: ostream& os)

{

PrintBits(& obj,sizeof obj,os); < br $>
}

#include< iostream>


int main()

{

long double array [4] = {241.126,632.225,2662.2523,23345.2352};


PrintObjBits(array,std :: cout);

}


-


Frederick Gotham


Robbie Hatley写道:


我正在努力想出一种方法来辨别C ++对象表示的实际

位模式(特别是

小型内置类型的对象),然后我想出了

。但是我想知道,有没有更简单的方法

这样做?这看起来很笨拙。



将对象复制到一个大小合适的unsigned char数组中,然后转换为
十六进制的unsigned chars。如果你真的需要二进制而不是十六进制,

转换是一个相当简单的练习。


I was struggling to come up with a way to discern the actual
bit patterns of the representations of C++ objects (esp.
objects of small built-in types), and I came up with the
following mess. But I''m wondering, is there an easier way
to do this? This seems so klunky.

#include <iostream>
#include <cmath>
template<typename T>
void Binary(T const & object)
{
size_t size = 8 * sizeof(object); // Size of object in bits.
unsigned long long int mask =
static_cast<unsigned long long int>
(pow(2.0, static_cast<double>(size - 1)) + 0.1);
unsigned long long int pattern =
*reinterpret_cast<const unsigned long long int*>(&object);
for( ; mask 0 ; mask >>= 1)
{
if(pattern & mask) std::cout << "1";
else std::cout << "0";
}
std::cout << std::endl;
return;
}
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/

解决方案

* Robbie Hatley:

I was struggling to come up with a way to discern the actual
bit patterns of the representations of C++ objects (esp.
objects of small built-in types), and I came up with the
following mess. But I''m wondering, is there an easier way
to do this? This seems so klunky.

#include <iostream>
#include <cmath>
template<typename T>
void Binary(T const & object)
{
size_t size = 8 * sizeof(object); // Size of object in bits.

Use CHAR_BITS or whatever it''s called: a byte isn''t necessarily 8 bits.

unsigned long long int mask =

C++ does not have a ''long long'' type, yet.

static_cast<unsigned long long int>
(pow(2.0, static_cast<double>(size - 1)) + 0.1);

Use left shift operator.

unsigned long long int pattern =
*reinterpret_cast<const unsigned long long int*>(&object);

You don''t know that sizeopf(object) <= sizeof(long long).

for( ; mask 0 ; mask >>= 1)
{
if(pattern & mask) std::cout << "1";
else std::cout << "0";

std::cout << !!(pattern & mask);

}
std::cout << std::endl;
return;

Unnecessary ''return''.

}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Robbie Hatley posted:

I was struggling to come up with a way to discern the actual
bit patterns of the representations of C++ objects


Here''s some code I wrote recently if you''re interested:

#include <cstddef>
#include <cassert>
#include <climits>
#include <ostream>

void PrintBits(void const * const mem,size_t amount_bytes,std::ostream &os)
{
assert( mem );
assert( amount_bytes );

char static str[CHAR_BIT + 1] = {};

unsigned char const *p = reinterpret_cast<unsigned char const*>(mem);

do
{
unsigned const byte_val = *p++;

char *pos = str;

unsigned to_and_with = 1U << CHAR_BIT - 1;

do *pos++ = byte_val & to_and_with ? ''1'' : ''0'';
while(to_and_with >>= 1);

os << str;

} while (--amount_bytes);
}
template<class T>
inline void PrintObjBits(T const &obj,std::ostream &os)
{
PrintBits(&obj,sizeof obj,os);
}
#include <iostream>

int main()
{
long double array[4] = { 241.126, 632.225, 2662.2523, 23345.2352 };

PrintObjBits(array,std::cout);
}

--

Frederick Gotham


Robbie Hatley wrote:

I was struggling to come up with a way to discern the actual
bit patterns of the representations of C++ objects (esp.
objects of small built-in types), and I came up with the
following mess. But I''m wondering, is there an easier way
to do this? This seems so klunky.

Copy the object into a suitably sized array of unsigned char, then dump
the unsigned chars in hex. If you really need binary rather than hex,
the conversion is a fairly simple exercise.


这篇关于对象表示的位模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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