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

x
Solved

Posted on 2003-03-03
Medium Priority
250 Views
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
Question by:Cryptos
• 2
• 2

LVL 86

Accepted Solution

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

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

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

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

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…
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.
Suggested Courses
Course of the Month13 days, 9 hours left to enroll