473,883 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_uint1 6(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}

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

double_to_uint1 6(&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 6646
>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_uint1 6(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.goog legroups.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_uint1 6(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}

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

double_to_uint1 6(&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.3333333333333 333
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_uint1 6(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}

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

double_to_uint1 6(&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_uint1 6(double *in, unsigned short int *out)
{
size_t index;

assert(CHAR_BIT == 8);
assert(sizeof(s hort) == 2);
assert(sizeof(d ouble) == 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_uint1 6(&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_uint1 6(double *in, unsigned short int *out)
{
out = (unsigned short int *)in;
}

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

double_to_uint1 6(&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_uint1 6(double *in, unsigned short int *out)
{
size_t index;

assert(CHAR_BIT == 8);
assert(sizeof(s hort) == 2);
assert(sizeof(d ouble) == 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
2709
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 (7.20.3.4). Something like this (include and checkings omitted): p = malloc(sizeof(*p) * 5); p = 1; p = 1; p = 1;
21
2295
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 idea? Could it be due to the lack of dynamic
72
3661
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
2447
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, strings for logging, and structures for data points. I want to use one function to read data from this block and one function to write data, for example: sram_read(OBJECT_IDENTIFIER) would return a pointer to the appriate object and
62
17903
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(); img.src = ; Then I use the image a few more times by adding it into an Array object:
3
5334
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". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
21
4662
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 error.
1
8016
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 understanding why and how a double can be represented in bits and bytes and which of the following can allow us to view the binary representation, unsigned char representation and then convert back to int please... #include<iostream> #include<iomanip>...
1
1543
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 values of an image(satellite image) and do some computation on it so I can classify them into dry grass, road, trees, water, building,etc. The text files contains 4 values for each pixel representing 4 bands(band 1,band 2 band 3, band 4) for each...
0
9953
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11166
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9588
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5808
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.