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 ) ;
}