Convert to binary base long double

  • Thread starter jose luis fernandez diaz
  • Start date
J

jose luis fernandez diaz

Hi,

The program below prints a unsigned short in binary base:


#include <iostream>

int main() {
unsigned short mask =0x1;
unsigned short us =0x0071;

mask<<=sizeof(unsigned short)*8-1;


for (int i = 0; i < sizeof(unsigned short)*8; i++)
{
cout << ((us & mask)? 1:0);
us <<= 1;
}

return 0;
}


How can I print the binary base of a long double ?


Thanks,
Jose Luis.
 
A

Asfand Yar Qazi

jose said:
Hi,

The program below prints a unsigned short in binary base:


#include <iostream>

int main() {
unsigned short mask =0x1;
unsigned short us =0x0071;

mask<<=sizeof(unsigned short)*8-1;


for (int i = 0; i < sizeof(unsigned short)*8; i++)
{
cout << ((us & mask)? 1:0);
us <<= 1;
}

return 0;
}


How can I print the binary base of a long double ?

I think long double should be 64 bits long... so cast to a 'long long*'
(if your compiler supports it) and away you go as above. I think :)
 
G

Gianni Mariani

jose said:
Hi,

The program below prints a unsigned short in binary base:


#include <iostream>

int main() {
unsigned short mask =0x1;
unsigned short us =0x0071;

mask<<=sizeof(unsigned short)*8-1;


for (int i = 0; i < sizeof(unsigned short)*8; i++)
{
cout << ((us & mask)? 1:0);
us <<= 1;
}

return 0;
}


How can I print the binary base of a long double ?


You need to know the format of floating point types for your platform.
Most modern systems use the IEEE floating point formats.
 
D

David Harmon

How can I print the binary base of a long double ?

The very thought of doing such a low-level and implementation
specific thing is naughty. Nevertheless...

Create a union type containing a long double and
array of char[sizeof(long double}]. Assign to the long
double member, then print the chars one by one. This yields
explicitly "undefined behavior", but has been known to work
for some implementation.
 
K

Kevin Goodsell

Asfand said:
I think long double should be 64 bits long... so cast to a 'long long*'
(if your compiler supports it) and away you go as above. I think :)

That is a very non-portable suggestion. Just performing the cast may
give undefined behavior.

-Kevin
 
K

Kevin Goodsell

David said:
How can I print the binary base of a long double ?


The very thought of doing such a low-level and implementation
specific thing is naughty. Nevertheless...

Create a union type containing a long double and
array of char[sizeof(long double}]. Assign to the long
double member, then print the chars one by one. This yields
explicitly "undefined behavior", but has been known to work
for some implementation.

Why use undefined behavior and risk complete failure when there's a
simple way to do it in a well-defined manner?

long double val = 12.34;
unsigned char bytes[sizeof(long double)];
memcpy(bytes, &val, sizeof(val));

Now 'bytes' contains the object-representation (C99 term for it) of val,
and you can print it out however you see fit.

-Kevin
 
J

Jack Klein

Hi,

The program below prints a unsigned short in binary base:


#include <iostream>

int main() {
unsigned short mask =0x1;
unsigned short us =0x0071;

mask<<=sizeof(unsigned short)*8-1;

This is hideous and makes non-portable assumptions. Include <climits>
and define mask like this:

unsigned int mask = USHRT_MAX - (USHRT_MAX >> 1);
for (int i = 0; i < sizeof(unsigned short)*8; i++)

Replace this with:

for ( ; max != 0; max >> 1)
{
cout << ((us & mask)? 1:0);
us <<= 1;

Eliminate the line above.
 
O

Old Wolf

Jack Klein said:
This is hideous and makes non-portable assumptions. Include <climits>
and define mask like this:

unsigned int mask = USHRT_MAX - (USHRT_MAX >> 1);


Replace this with:

for ( ; max != 0; max >> 1)

for ( ; mask != 0; mask >>= 1)
Eliminate the line above.

This has caught me many a time :) (especially on compilers
that fail to give it a diagnostic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top