473,782 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(u nsigned 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 3099
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(u nsigned 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(u nsigned 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(u nsigned 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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #7
Jack Klein <ja*******@spam cop.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
2578
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 but I'm not sure what to do to include the decimal part. //----------------------------------------------------------- const char ccBaseChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char sResult = {0}; int iBase;
5
7066
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: "9567081354794432" should be converted to the seven bytes: 0x21FD35B5B095C0 Thanks in advance Sam
11
6307
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 ioctl() is expecting a 32-bit integer value, and 0xc0047a80 has the high-order bit set. I'm assuming Python thinks it's a signed value. How do I tell Python that 0xc0047a80 is an unsigned 32-bit value?
9
90461
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 searched numberless websites but nothing could answer my question. I would be pleased to get some help. Regards
7
7122
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 without strtol or s/printf function. Thanks, whatluo.
3
2259
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, char *IN_msg ); DLLEXPORT long F_BDO_CalculAXplusB(short int *IN_Tab_entree ,int IN_taille , double *OUT_Tab_sortie_freq , double *OUT_Tab_sortie, double IN_A,
0
2146
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" targetNamespace="http://swaMaragatoNS" xmlns:tns="http://swaMaragatoNS" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
8
3671
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 appreciate a prompt reply because I have a python assessment to submit. Thanks, Thomas
22
11782
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.