Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

bitwise access to a double?

Posted on 2003-03-03
4
Medium Priority
?
250 Views
Last Modified: 2010-04-01
Hello

i need to convert a double into bools so that the 64 bits that correspond to a double are stored in booleans. So I tried to shift or mask it. But this seems not to work.

Any C++ hints?

Im programming with MSVC 6.0

Tanx
0
Comment
Question by:Cryptos
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 8059560
You could use a union to accomplish that:

 typedef unsigned char BYTE;
 typedef union _dbl_t
 {
    double d;
    BYTE b[8];
 } dbl_t;

double d = 3.141;
dbl_t dbl;

dbl.d = d;

// now access the bits via the BYTE member of the union:
bool lowest_bit = dbl.b [ 0] & 0x01;
0
 

Author Comment

by:Cryptos
ID: 8059636
could i make this access directly like:

typedef bool BIT;
typedef union _dbl_t
{
   double d;
   BIT b[64];
} dbl_t;

double d = 3.141;
dbl_t dbl;

dbl.d = d;

bool bit0=dbl.b[0];

That would be nice...

thanks for that

Urs
0
 

Author Comment

by:Cryptos
ID: 8059646
ah of course this doesnt work, as the bool takes 8bits!!

anyway the byte version is nice enough

tanx
0
 
LVL 86

Expert Comment

by:jkr
ID: 8059670
How would you realize

  BIT b[64];

?

However, what would work better for you is a std::bitset, e.g.

#include <bitset>

typedef union _dbl_t
{
   double d;
   long l[2];
} dbl_t;

double d = 3.141;
dbl_t dbl;

dbl.d = d;

std::bitset bs ( dbl.l[0]);

bool b0 = bs [0];
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question