472,790 Members | 1,332 Online

# Convert to binary base long double

Hi,

The program below prints a unsigned short in binary base:
#include <iostream>

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

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.
Jul 22 '05 #1
7 3013
jose luis fernandez diaz wrote:
Hi,

The program below prints a unsigned short in binary base:
#include <iostream>

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

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 :-)

--
http://www.it-is-truth.org/

Jul 22 '05 #2
jose luis fernandez diaz wrote:
Hi,

The program below prints a unsigned short in binary base:
#include <iostream>

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

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.

Jul 22 '05 #3
On 23 Jan 2004 01:48:22 -0800 in comp.lang.c++,
jo**********************@yahoo.es (jose luis fernandez diaz) was alleged
to have written:
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.
Jul 22 '05 #4
Asfand Yar Qazi wrote:

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
--
My email address is valid, but changes periodically.
Jul 22 '05 #5
David Harmon wrote:
On 23 Jan 2004 01:48:22 -0800 in comp.lang.c++,
jo**********************@yahoo.es (jose luis fernandez diaz) was alleged
to have written:
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
--
My email address is valid, but changes periodically.
Jul 22 '05 #6
On 23 Jan 2004 01:48:22 -0800, jo**********************@yahoo.es (jose
luis fernandez diaz) wrote in comp.lang.c++:
Hi,

The program below prints a unsigned short in binary base:
#include <iostream>

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

This is hideous and makes non-portable assumptions. Include <climits>

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.
}

return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #7
Jack Klein <ja*******@spamcop.net> wrote in message news:<he********************************@4ax.com>. ..
This is hideous and makes non-portable assumptions. Include <climits>

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.
}

This has caught me many a time :) (especially on compilers
that fail to give it a diagnostic)
Jul 22 '05 #8

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

### Similar topics

 1 by: ferran | last post by: Hi, does anybody know how to convert in C++ from base 10 to any other base without loosing the decimal part of the actual value? I came up with this algorithm to convert from decimal to any base... 5 by: Sam Smith | last post by: Hi, is there a function or a "well-known" algorithm which converts a number of random length represented as an array of bytes to its binary format? For example: a 16 byte long array:... 11 by: Grant Edwards | last post by: I give up, how do I make this not fail under 2.4? fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pack("HBB",0x1c,0x00,0x00)) I get an OverflowError: long int too large to convert to int ... 9 by: FalkoG | last post by: Hello colleague I want to convert a floating number for example 5236.9856982 to a hexadecimal number. I'd tried several things but my problem is to convert the position after decimal point. I... 7 by: whatluo | last post by: Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done... 3 by: Eric BOUXIROT | last post by: hi, i must convert all of these eVC++ prototypes to use with VB.NET.... DLLEXPORT long F_BDO_MessageBoxOK(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_MessageBoxOUINON(char *IN_title,... 0 by: robert | last post by: Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: