Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 11th, 2006, 8:52 AM   #1
ionexchange
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 ionexchange is on a distinguished road
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);
I compile and get an error
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
ionexchange is offline   Reply With Quote
Old Feb 11th, 2006, 9:58 AM   #2
sunnypalsingh
Programmer
 
sunnypalsingh's Avatar
 
Join Date: Dec 2005
Posts: 33
Rep Power: 0 sunnypalsingh is on a distinguished road
Can't use doubles/floats with bitwise operators. Use integer types instead.
__________________
The only real valuable thing is intuition.
-Albert Einstein
sunnypalsingh is offline   Reply With Quote
Old Feb 11th, 2006, 10:40 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 11th, 2006, 10:58 AM   #4
ionexchange
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 ionexchange is on a distinguished road
Quote:
Originally Posted by DaWei
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.
I believe that double is represented as two unsigned long int's, so I guess I'm left with using two unsigned long int's and overloading the operators?

Thanks DaWei and sunnypalsingh for your help.
Milton
ionexchange is offline   Reply With Quote
Old Feb 11th, 2006, 11:01 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
I believe that double is represented as two unsigned long int's,
You believe wrongly unless you have a reallllllyyyyyy strange machine.
Quote:
Originally Posted by DaWei
I'd recommend you investigate exactly what a double is and how it's represented in your system...
__________________
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
DaWei is offline   Reply With Quote
Old Feb 11th, 2006, 11:35 AM   #6
ionexchange
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 ionexchange is on a distinguished road
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;
}
I use
for (unsigned int a = 0; a < 64; a++){
    std::cout << a << " " << ((double)(1<<a)) << std::endl;
}
to determine see how the bits are represented in double. The output for a = 0 through a = 31 is identical for the output for a=32 through a=65. Is there another way to determine what you are refering to investigate exactly what a double is and how it's represented in my system.

Milt
ionexchange is offline   Reply With Quote
Old Feb 11th, 2006, 12:32 PM   #7
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Take a look at the IEEE Floating point specification, and how float and double's are represented in binary. Then you'll understand.
andro is offline   Reply With Quote
Old Feb 11th, 2006, 1:00 PM   #8
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
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)
andro is offline   Reply With Quote
Old Feb 11th, 2006, 1:33 PM   #9
ionexchange
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 ionexchange is on a distinguished road
Quote:
Originally Posted by andro
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)
Thanks, now I understand what I read.

Milt
I have seen the light, and it BURNS!
ionexchange is offline   Reply With Quote
Old Feb 11th, 2006, 1:42 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC