![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
bitwise operators and type double
Hello,
I'm tring to use bitwise operators like <<, |, &, ^ with a double data type. double foo = 0; double bar = 0; double huh = 0; huh = (foo & bar); invalid operands of type "double" and "double" to binary operator& I get similar errors when I use | and ^. When I use << I get results as if I'm dealing with two signed long ints. Somewhere in the dusty area of my mind I recall that double is just that, two signed long ints. I need a data type that has 64 bits that I can use these bitwise operators. Is there one out there or do I have to create my own and overload those operators? Thanks, Milton |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2005
Posts: 33
Rep Power: 0
![]() |
Can't use doubles/floats with bitwise operators. Use integer types instead.
__________________
The only real valuable thing is intuition. -Albert Einstein |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
If you want to mess with a double bitwise, you'll have to cast it to something else. Before you do that, I'd recommend you investigate exactly what a double is and how it's represented in your system, and what endianness is associated with your machine.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 | |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
Quote:
Thanks DaWei and sunnypalsingh for your help. Milton |
|
|
|
|
|
|
#5 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
Okay,
So I'm confused. I determine my machine represents float with big endianness by using the following code. #define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1
int main()
{
float i = 1;
char *p = (char *) &i;
if (p[0] == 1) // Lowest address contains the least significant byte
std::cout << "little" << std::endl;
else
std::cout << "big" << std::endl;
return 1;
}for (unsigned int a = 0; a < 64; a++){
std::cout << a << " " << ((double)(1<<a)) << std::endl;
}Milt |
|
|
|
|
|
#7 |
|
Professional Programmer
|
Take a look at the IEEE Floating point specification, and how float and double's are represented in binary. Then you'll understand.
|
|
|
|
|
|
#8 |
|
Professional Programmer
|
Bytes:
31-30 sign 30-23 exponent 23-0 signifcand (-1)^s * (1 + significand) * 2^(exp-127) |1|1000 0001|111 0000 0000 0000 0000 0000| (-1)^1 * (1+.111) * 2^(129-127) -1 * (1.111) * 2^2 -111.1 (base 2) =7.5 (base 10) |
|
|
|
|
|
#9 | |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
Quote:
Milt I have seen the light, and it BURNS! |
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
On my machine, the example given above represents a float. A double is represented in a 64-bit value with a similar structure but a larger number of bits for the mantissa and exponent. This is why you need to investigate for YOUR machine. The IEEE specification is a very good start, and will definitely give you the overall picture, but not all implementations are in accordance with it. You can appreciate that for any given size of binary thangy, only so much information can be encoded. This is why floating point values give you more range, but less precision.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|