473,388 Members | 1,001 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Get memory representation of double

Hi!

I want to get the representation in memory of a variable of type
double and put it in an array of four unsigned short int (I'm on a 32-
bits Windows architecture so a double is 64-bits and an unsigned short
int is 16-bits).

For example, the double precision representation of 1/3 is 3fd5 5555
5555 5555 (hex).

I tried to do a simple cast from a pointer of type double to a pointer
to an array of unsigned short int but does not seem to work. I'm not
very good a C programming so help is very much appreciated.
#include <stdio.h>

void double_to_uint16(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}

main()
{
double a = 0.3333333333333333;
unsigned short int b[4];

double_to_uint16(&a, b);

printf("1: %hx\n", b[0]);
printf("2: %hx\n", b[1]);
printf("3: %hx\n", b[2]);
printf("4: %hx\n", b[3]);
}
The program above gives the following printout:

1: f000
2: 6113
3: 0
4: 0

But I expected to get:

1: 3fd5
2: 5555
3: 5555
4: 5555

Aug 4 '07 #1
5 6591
>For example, the double precision representation of 1/3 is 3fd5 5555
>5555 5555 (hex).

I tried to do a simple cast from a pointer of type double to a pointer
to an array of unsigned short int but does not seem to work. I'm not
very good a C programming so help is very much appreciated.
#include <stdio.h>

void double_to_uint16(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
Don't you mean:
*out = (unsigned short int *)in;
and this will only copy *one* short int, so you want to copy
sizeof(double)/sizeof(unsigned short int) times, moving to
the next unsigned short int each time.
>}
Aug 4 '07 #2

<an************@home.sewrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
Hi!

I want to get the representation in memory of a variable of type
double and put it in an array of four unsigned short int (I'm on a 32-
bits Windows architecture so a double is 64-bits and an unsigned short
int is 16-bits).

For example, the double precision representation of 1/3 is 3fd5 5555
5555 5555 (hex).

I tried to do a simple cast from a pointer of type double to a pointer
to an array of unsigned short int but does not seem to work. I'm not
very good a C programming so help is very much appreciated.
#include <stdio.h>

void double_to_uint16(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}

main()
{
double a = 0.3333333333333333;
unsigned short int b[4];

double_to_uint16(&a, b);

printf("1: %hx\n", b[0]);
printf("2: %hx\n", b[1]);
printf("3: %hx\n", b[2]);
printf("4: %hx\n", b[3]);
}
The program above gives the following printout:

1: f000
2: 6113
3: 0
4: 0

But I expected to get:

1: 3fd5
2: 5555
3: 5555
4: 5555
Why?
#include <stdio.h>

int main(void)
{
double d = 1.0 / 3;
unsigned char *p = (unsigned char *)&d;

printf("type 'double' object 'd' at address %p has value of %.16f\n",
(void*)&d, d);

size_t i = 0;

for(; i < sizeof d; ++i)
printf("byte %lu at address %p == %X\n",
(unsigned int)i, (void*)(p + i), p[i]);

return 0;
}

Output (MS VC++ 2005 Express, Pentium IV):

type 'double' object 'd' at address 0013FF6C has value of 0.3333333333333333
byte 0 at address 0013FF6C == 55
byte 1 at address 0013FF6D == 55
byte 2 at address 0013FF6E == 55
byte 3 at address 0013FF6F == 55
byte 4 at address 0013FF70 == 55
byte 5 at address 0013FF71 == 55
byte 6 at address 0013FF72 == D5
byte 7 at address 0013FF73 == 3F

-Mike
Aug 4 '07 #3
an************@home.se wrote:
Hi!

I want to get the representation in memory of a variable of type
double and put it in an array of four unsigned short int (I'm on a 32-
bits Windows architecture so a double is 64-bits and an unsigned short
int is 16-bits).
If you want the bit representation of an object, the easiest way is to
access it as an array of unsigned char.
For example, the double precision representation of 1/3 is 3fd5 5555
5555 5555 (hex).
The representation is implementation specific.
I tried to do a simple cast from a pointer of type double to a pointer
to an array of unsigned short int but does not seem to work. I'm not
very good a C programming so help is very much appreciated.
Try this -

#include <stdio.h>

int main(void) {
double d = 1.0/3.0;
unsigned char *drep = 0;
int ctr;

printf("d = %f\n", d);
drep = (unsigned char *)&d;
for(ctr = 0; ctr < sizeof(double); ctr++) {
printf("d (byte %d) = %x\n", ctr, drep[ctr]);
}
return 0;
}

Output

d = 0.333333
d (byte 0) = 55
d (byte 1) = 55
d (byte 2) = 55
d (byte 3) = 55
d (byte 4) = 55
d (byte 5) = 55
d (byte 6) = d5
d (byte 7) = 3f
Aug 4 '07 #4
an************@home.se wrote:
>
Hi!

I want to get the representation in memory of a variable of type
double and put it in an array of four unsigned short int (I'm on a 32-
bits Windows architecture so a double is 64-bits and an unsigned short
int is 16-bits).

For example, the double precision representation of 1/3 is 3fd5 5555
5555 5555 (hex).

I tried to do a simple cast from a pointer of type double to a pointer
to an array of unsigned short int but does not seem to work. I'm not
very good a C programming so help is very much appreciated.

#include <stdio.h>

void double_to_uint16(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}

main()
{
double a = 0.3333333333333333;
unsigned short int b[4];

double_to_uint16(&a, b);

printf("1: %hx\n", b[0]);
printf("2: %hx\n", b[1]);
printf("3: %hx\n", b[2]);
printf("4: %hx\n", b[3]);
}

The program above gives the following printout:

1: f000
2: 6113
3: 0
4: 0

But I expected to get:

1: 3fd5
2: 5555
3: 5555
4: 5555
/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>
#include <assert.h>

void double_to_uint16(double *in, unsigned short int *out)
{
size_t index;

assert(CHAR_BIT == 8);
assert(sizeof(short) == 2);
assert(sizeof(double) == 8);
for (index = 0; index != 4; ++index) {
out[index] = ((unsigned short *)in)[index];
}
}

int main(void)
{
double a = 1.0 / 3;
unsigned short int b[4];

double_to_uint16(&a, b);
printf("1: %hx\n", b[0]);
printf("2: %hx\n", b[1]);
printf("3: %hx\n", b[2]);
printf("4: %hx\n", b[3]);
return 0;
}

/* END new.c */
--
pete
Aug 4 '07 #5
pete wrote:
>
an************@home.se wrote:

Hi!

I want to get the representation in memory of a variable of type
double and put it in an array of four unsigned short int (I'm on a 32-
bits Windows architecture so a double is 64-bits and an unsigned short
int is 16-bits).

For example, the double precision representation of 1/3 is 3fd5 5555
5555 5555 (hex).

I tried to do a simple cast from a pointer of type double to a pointer
to an array of unsigned short int but does not seem to work. I'm not
very good a C programming so help is very much appreciated.

#include <stdio.h>

void double_to_uint16(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}

main()
{
double a = 0.3333333333333333;
unsigned short int b[4];

double_to_uint16(&a, b);

printf("1: %hx\n", b[0]);
printf("2: %hx\n", b[1]);
printf("3: %hx\n", b[2]);
printf("4: %hx\n", b[3]);
}

The program above gives the following printout:

1: f000
2: 6113
3: 0
4: 0

But I expected to get:

1: 3fd5
2: 5555
3: 5555
4: 5555

/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>
#include <assert.h>

void double_to_uint16(double *in, unsigned short int *out)
{
size_t index;

assert(CHAR_BIT == 8);
assert(sizeof(short) == 2);
assert(sizeof(double) == 8);
for (index = 0; index != 4; ++index) {
out[index] = ((unsigned short *)in)[index];
}
}
That for loop, makes an assumption about alignment
that may not be true.

For raw memory representation,
you can't go wrong with bytes of unsigned char.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
double a = 1.0 / 3;
size_t i;

for (i = 0; i != sizeof a; ++i) {
printf("%u: %x\n", (unsigned)i, ((unsigned char *)&a)[i]);
}
return 0;
}

/* END new.c */
--
pete
Aug 4 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Holger Hasselbach | last post by:
- The value of the object allocated by the malloc function is used (7.20.3.3). - The value of any bytes in a new object allocated by the realloc function beyond the size of the old object are used...
21
by: Marky C | last post by:
atof is not working. double a = atof("12.345"); a gets set to 12.000 I am working on a toshiba micro. The data map has no space allocated to it for dynamic memory. Does anyone have an...
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
21
by: =?UTF-8?B?TWFydGluIFDDtnBwaW5n?= | last post by:
Hello, I´m using a very large 2-dimensional double matrix (45.000x45.000) in my program. At the initialization of the matrix: double matrix = new double I am getting an out of memory...
1
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
1
by: xHolyWrath | last post by:
Hi everyone! I'm new to this forum so any advise will be very much appreciated. I'm currently creating a C program that would read a text file full of integer values that represents pixel spectrum...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.