在c ++中交换位为双 [英] Swap bits in c++ for a double

查看:166
本文介绍了在c ++中交换位为双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从大字符串转换成小字符串。一种方法是使用

  double val,tmp = 5.55; 

((unsigned int *)& val)[0] = ntohl(((unsigned int *)& tmp)[1]);
((unsigned int *)& val)[1] = ntohl(((unsigned int *)& tmp)[0]);

但是,我得到一个警告:取消引用类型惩罚指针将破坏严格的别名规则,我不想关闭此警告。



另一种方法是:

  #define ntohll(x)(((uint64_t)((uint32_t)((x 32)> 32)))< 32)| ntohl(((uint32_t )(x> 32))))

val =(double)bswap_64(unsigned long long(tmp)); //或
val =(double)ntohll(unsigned long long(tmp));

但是失去小数。
任何人都知道一个很好的方法来交换一个double而不使用for循环?

解决方案

可能会尝试这样的事情:

 模板< typename T> 
void swap_endian(T& pX)
{
//应该静态断言T是一个POD

char& raw = reinterpret_cast< char&>(pX);
std :: reverse(& raw,& raw + sizeof(T));
}

短而甜(相对未经测试)。编译器将进行所有必要的优化。上述对于任何POD类型都是明确定义的,不依赖于任何实现细节。



复制版本,当您不想修改参数:

 模板< typename T> 
T swap_endian_copy(T pX)
{
swap_endian(pX);
return pX;
}


Im trying to change from big endian to little endian on a double. One way to go is to use

double val, tmp = 5.55;

((unsigned int *)&val)[0] = ntohl(((unsigned int *)&tmp)[1]);
((unsigned int *)&val)[1] = ntohl(((unsigned int *)&tmp)[0]);

But then I get a warning: "dereferencing type-punned pointer will break strict-aliasing rules" and I dont want to turn this warning off.

Another way to go is:

#define ntohll(x) ( ( (uint64_t)(ntohl( (uint32_t)((x << 32) >> 32) )) << 32) | ntohl( ((uint32_t)(x >> 32)) ) ) 

val = (double)bswap_64(unsigned long long(tmp)); //or
val = (double)ntohll(unsigned long long(tmp));

But then a lose the decimals. Anyone know a good way to swap the bits on a double without using a for loop?

解决方案

I'd probably try something like this:

template <typename T>
void swap_endian(T& pX)
{
    // should static assert that T is a POD

    char& raw = reinterpret_cast<char&>(pX);
    std::reverse(&raw, &raw + sizeof(T));
}

Short and sweet (and relatively untested). The compiler will make all the necessary optimizations. The above is well-defined for any POD type, and doesn't rely on any implementation details.

A copy version, for when you don't want to modify the argument:

template <typename T>
T swap_endian_copy(T pX)
{
    swap_endian(pX);
    return pX;
}

这篇关于在c ++中交换位为双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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