472,790 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,790 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 3013
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?
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.