Writing a float or double's byte values

P

Philipp

Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

I read that on win32 the representation of 8byte double is
byte1 byte2 byte3 byte4 byte8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM

with S= signe bit
X = exponant bits
M = mantissa bits

That's the values I'd like to get.
Thanks for your help Phil
 
J

John Harrison

Philipp said:
Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

union
{
unsigned char b[8];
double d;
} u;

u.d= 1.2345;
cout << hex << setfill('0');
for (int i = 0; i < 8; ++i)
cout << u.b << ' ';

Untested code.

john
 
K

Kari Laitinen

Philipp said:
Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

I read that on win32 the representation of 8byte double is
byte1 byte2 byte3 byte4 byte8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM

with S= signe bit
X = exponant bits
M = mantissa bits

That's the values I'd like to get.
Thanks for your help Phil

The program that is copied below prints an int variable
in binary form. It could be modified to work with double
variables. but probably you need to use pointers to
access the bytes of a double. The following MAY work

int* first_four_bytes = &given_double_variable ;
int* last_four_bytes = first_four_bytes + 1 ;

--
Mr. (Dr.) Kari Laitinen
Oulu Institute of Technology, Finland
http://www.oamk.fi/~karil/
http://www.naturalprogramming.com/


// binary.cpp (c) 1998-2002 Kari Laitinen

// This program is from a book. More information at
// http://www.naturalprogramming.com/cppbook.html

#include <iostream.h>

void print_in_binary_form( int given_integer )
{
// This program works only with 32-bit int variables.
// To make this program work with 16-bit int variables,
// you should use initial mask 0x8000 and let the loop
// be executed only 16 times.

unsigned int bit_mask = 0x80000000 ;
unsigned int one_bit_in_given_integer ;

for ( int bit_counter = 0 ;
bit_counter < 32 ;
bit_counter ++ )
{
one_bit_in_given_integer = given_integer & bit_mask ;

if ( one_bit_in_given_integer == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}


int main()
{
unsigned int test_number = 0x9A9A ;

cout << "\n Original test number: " ;
print_in_binary_form( test_number ) ;

cout << "\n Twice left-shifted form: " ;
test_number = test_number << 2 ;
print_in_binary_form( test_number ) ;

cout << "\n Back to original form: " ;
test_number = test_number >> 2 ;
print_in_binary_form( test_number ) ;

cout << "\n Last four bits zeroed: " ;
test_number = test_number & 0xFFF0 ;
print_in_binary_form( test_number ) ;

cout << "\n Last four bits to one: " ;
test_number = test_number | 0x000F ;
print_in_binary_form( test_number ) ;

cout << "\n A complemented form: " ;
test_number = ~test_number ;
print_in_binary_form( test_number ) ;

cout << "\n Exclusive OR with 0xF0F0:" ;
test_number = test_number ^ 0xF0F0 ;
print_in_binary_form( test_number ) ;
}
 
K

Kari Laitinen

Philipp said:
Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

I read that on win32 the representation of 8byte double is
byte1 byte2 byte3 byte4 byte8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM

with S= signe bit
X = exponant bits
M = mantissa bits

That's the values I'd like to get.
Thanks for your help Phil


Below is a version of my program which seems to
do the job. I compiled it with Borland C++ 5.5.1.

The advice in my previous message were a little bit
inaccurate.

--
Mr. (Dr.) Kari Laitinen
Oulu Institute of Technology, Finland
http://www.oamk.fi/~karil/ + 358 40 566 0869
http://www.naturalprogramming.com/

// double_to_binary.cpp (c) 1998-2002 Kari Laitinen

// This program is from a book. More information at
// http://www.naturalprogramming.com/cppbook.html

#include <iostream.h>

void print_in_binary_form( int given_integer )
{
// This program works only with 32-bit int variables.
// To make this program work with 16-bit int variables,
// you should use initial mask 0x8000 and let the loop
// be executed only 16 times.

unsigned int bit_mask = 0x80000000 ;
unsigned int one_bit_in_given_integer ;

for ( int bit_counter = 0 ;
bit_counter < 32 ;
bit_counter ++ )
{
one_bit_in_given_integer = given_integer & bit_mask ;

if ( one_bit_in_given_integer == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}


int main()
{

double some_double_number = 1234.567 ;

int* first_four_bytes = (int*) &some_double_number ;
int* last_four_bytes = first_four_bytes + 1 ;

print_in_binary_form( *first_four_bytes ) ;
print_in_binary_form( *last_four_bytes ) ;


}
 
F

Fraser Ross

double some_double_number = 1234.567 ;
int* first_four_bytes = (int*) &some_double_number ;
int* last_four_bytes = first_four_bytes + 1 ;

An implementation does not have to use 32 bits with ints. I think they do
have to use 64 bits for double. I would reinterpret_cast a pointer to
double to a pointer to unsigned __int64 or whatever keyword a compiler uses.
__int64 is a Borland keyword. It isn't portable but it is easily changed
and quaranteed to produce an error if a compiler hasn't got the keyword.

Fraser.
 
P

Philipp

Kari said:
Below is a version of my program which seems to
do the job. I compiled it with Borland C++ 5.5.1.

The advice in my previous message were a little bit
inaccurate.

Both answers provide what I was looking for and work great. Thank you
very much!
Best regards Phil
 
K

Kari Laitinen

Philipp said:
Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

I read that on win32 the representation of 8byte double is
byte1 byte2 byte3 byte4 byte8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM

with S= signe bit
X = exponant bits
M = mantissa bits

That's the values I'd like to get.
Thanks for your help Phil


This is my third solution to your
problem. Below is a copy of a program that
should print the bytes of a double variable
in correct order. My previous solution was
such that it works only in a few computers.

I hope this works. There are, however, no errors in
the programs of the mentioned book.

--
Mr. (Dr.) Kari Laitinen
Oulu Institute of Technology, Finland
http://www.naturalprogramming.com/


// double_to_binary.cpp (c) 1998-2004 Kari Laitinen

// This program is from a book. More information at
// http://www.naturalprogramming.com/cppbook.html

#include <iostream.h>

void print_in_binary_form( char given_byte )
{
unsigned char bit_mask = 0x80 ;
unsigned char one_bit_in_given_byte ;

for ( int bit_counter = 0 ;
bit_counter < 8 ;
bit_counter ++ )
{
one_bit_in_given_byte = given_byte & bit_mask ;

if ( one_bit_in_given_byte == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}


void print_in_binary_form( int given_integer )
{
// This program works only with 32-bit int variables.
// To make this program work with 16-bit int variables,
// you should use initial mask 0x8000 and let the loop
// be executed only 16 times.

unsigned int bit_mask = 0x80000000 ;
unsigned int one_bit_in_given_integer ;

for ( int bit_counter = 0 ;
bit_counter < 32 ;
bit_counter ++ )
{
one_bit_in_given_integer = given_integer & bit_mask ;

if ( one_bit_in_given_integer == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}


int main()
{
cout << "\n\n" ;

double test_number = 123.456 ;

char* byte_in_test_number = (char*) &test_number ;

for ( int byte_counter = 0 ;
byte_counter < sizeof( double ) ;
byte_counter ++ )
{
cout << " " ;
print_in_binary_form( *byte_in_test_number ) ;
byte_in_test_number ++ ;
}

cout << "\n\n" ;

cout << "\n The following is not correct: \n" ;

int* first_four_bytes = (int*) &test_number ;
int* last_four_bytes = first_four_bytes + 1 ;

print_in_binary_form( *first_four_bytes ) ;

cout << " " ;

print_in_binary_form( *last_four_bytes ) ;

}
 
O

Old Wolf

John Harrison said:
Philipp said:
Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

union
{
unsigned char b[8];
double d;
} u;

u.d= 1.2345;
cout << hex << setfill('0');
for (int i = 0; i < 8; ++i)
cout << u.b << ' ';


Undefined behaviour (you access u.b but the last member set was u.d)
Also you should replace '8' with 'sizeof(double)'.

Better would be:

double d = 1.2345;
for (int i = 0; i != sizeof(double); ++i)
cout << ((unsigned char *)&d) << ' ';

or some optimised variation of that.
 

Members online

Forum statistics

Threads
474,179
Messages
2,570,956
Members
47,509
Latest member
Jack116

Latest Threads

Top