简体   繁体   English

通过附加两个int的二进制表示来创建double

[英]Create double by appending binary representations of two ints

I'm trying to create a double by appending the binary representations of two int s. 我试图通过附加两个int的二进制表示来创建一个double This is what I have now: 这就是我现在拥有的:

int i = arc4random();
*(&i+1) = arc4random();
double d = *(double*)&i;

I hoped the double pointer would also use the value in the &i+1 address, and this is indeed the case. 我希望double指针也会使用&i+1地址中的值,这确实是这种情况。 When I print the binary representations of i and *(&i+1) and compare them to the binary representation of d , d is composed by appending *(&i+1) and i . 当我打印i*(&i+1)的二进制表示并将它们与d的二进制表示进行比较时,d由附加*(&i+1)i So *(&i+1) comes first?! 所以*(&i+1)首先出现?! Why is this the case? 为什么会这样?

EDIT: also take a look at the answers of Juan Catalan and Mark Segal to know what's the right way of doing what I did using unions. 编辑:还要看看Juan Catalan和Mark Segal的答案,知道使用工会做什么的正确方法。

Use a union instead. 改为使用union You still could generate a NaN But won't have memory management issues. 您仍然可以生成NaN但不会有内存管理问题。

This is the whole purpose of a union , share a memory block among several different types. 这是一个的整个目的union ,分享几种不同类型之间的内存块。

Make sure in your architecture sizeof(double) is equal to twice sizeof(int) 确保在您的体系结构中sizeof(double)等于sizeof(int)两倍

You could either use a union, like this: 您可以使用联合,如下所示:

typedef union {
    double d;
    struct {
        int a;
        int b;
    } integers;
} double_generator;

int main(void) {
    double_generator g;
    g.integers.a = arc4random();
    g.integers.b = arc4random();

    // g.d is now a "randomly generated double"

    return 0;
}

Or "manually", possibly unsafely, do the job yourself: 或者“手动”,可能不安全,自己完成这项工作:

int main(void) {
    double d;
    *((int *)&d) = arc4random();
    *(((int *)&d) + 1) = arc4random();
    printf("%f", d);
    return 0;
}

In both cases, you simply address the same memory as if it was int or double . 在这两种情况下,您只需处理相同的内存,就像它是intdouble This could generate every possible double value, including NaN or Infinity . 这可以生成每个可能的double值,包括NaNInfinity

However, this code assumes that sizeof(double) is (at least) twice as sizeof(int) . 但是,此代码假定sizeof(double)是(至少) sizeof(int)两倍。 If it isn't, you must introduce different code to handle that. 如果不是,则必须引入不同的代码来处理它。

When I print the binary representations of i and *(&i+1) and compare them to the binary representation of d, d is composed by appending *(&i+1) and i. 当我打印i和*(&i + 1)的二进制表示并将它们与d的二进制表示进行比较时,d由附加*(&i + 1)和i组成。 So *(&i+1) comes first?! 所以*(&i + 1)首先出现?! Why is this the case? 为什么会这样?

the actual ordering of the bytes depends upon the 'Endian-ness' of the underlying hardware architecture. 字节的实际排序取决于底层硬件架构的“Endian-ness”。

with little Endian architecture: 与小Endian架构:

1) the lowest address byte contains the least significant 8 bits of the total variable  
2) the highest address byte contains the most significant 8 bits of the total variable.

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

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