473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Convert to binary base long double

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

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

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.
To contact me please use the address from a recent posting.
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.
To contact me please use the address from a recent posting.
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 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.
}

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


for ( ; mask != 0; mask >>= 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: <definitions name="MaragatoService"...
8
by: te509 | last post by:
Hi guys, does anybody know how to convert a long sequence of bits to a bit-string? I want to avoid this: '949456129574336313917039111707606368434510426593532217946399871489' I would...
22
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.