Solved

bitwise operation on double

Posted on 1998-07-28
18
237 Views
Last Modified: 2010-04-15
How can I convert a 8byte number (double) to two 4 bytes (unsigned longs) number ?
ex.   double   d;
       unsigned long   hi;
       unsigned long   lo;
       d = atof("112228383499478777277223");
       hi = first 4 bytes of d
       lo = second 4 bytes of d
0
Comment
Question by:bsmoam1
  • 5
  • 4
  • 3
  • +5
18 Comments
 

Author Comment

by:bsmoam1
Comment Utility
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
hi = ((unsigned long *)&d)[0];
lo = ((unsigned long *)&d)[1];
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
ozo why didn't you post this as an answer? it is absolutely perfect.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Because the operation is implementation defined, and so may not be guaranteed to work on bsmoam1's machine.
And even if it does, I'm not quite sure if this is what bsmoam1 really wants to do.
I await comment.
0
 
LVL 3

Expert Comment

by:elfie
Comment Utility
You have to take into account that the byte order for storing binary types (int, short, long) differs on PC's and most UNIX systems (Sun, HP, ...)

If you have a short that has a value s=1; and you take ozo's algorithm for converting it to char hi=((unsigned char*)&s)[0];


you will find that hi equals 1 on PC, and 0 on HP, Sun, ...
lo has the opposite value.

0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
It isn't possible to make a conversion like this machine independent, and the bsmoam1 did
not ask that he wants it to have a special order. So i think the answer ozo gave should be right ... or what?
0
 
LVL 3

Expert Comment

by:elfie
Comment Utility
using htons(3) calls can make it portable among different machines. On most unix systems these calls doesn't do a thing. But on pc systems the should change the byte order pf the passed long, int, short, ... .

0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
IMO, before discussing the size of int and the endian order issues, it should be noted that bitwise operations on doubles are not very orthodox.

-julio
0
 

Accepted Solution

by:
halen earned 100 total points
Comment Utility
Try this, it always works for me:

hi = (unsigned long)d >> 32;          //  shifts upper 32 bits(4 bytes) to lower 32 bits
                                                   //  zeros are shifted into the upper 32 bits hence
                                                   //  8888 7777 6666 5555 hex becomes
                                                   //  0000 0000 8888 7777 hex which is simply
                                                   //  8888 7777 hex when cast as a unsigned long.

lo = (unsigned long)d & 4294967295;  //  4294967295 decimal = FFFFFFFF hex.
                                                       //  By doing the bitwise AND operation of the
                                                       //  double and FFFFFFFF hex, the upper 32
                                                       //  bits become zero leaving us with the lower
                                                       //  part which is our answer.

To follow this approach it helps to have studied digital design or at least converting
numbers between decimal, hexidecimal and binary.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Expert Comment

by:snoegler
Comment Utility
No comment :)
0
 
LVL 3

Expert Comment

by:xyu
Comment Utility
:)
union Hehe {
  double d;
  struct HoHo{
    long l1;
    long l2;
  } _HoHo;
} _Hehe;

:)

0
 
LVL 1

Expert Comment

by:newexpert
Comment Utility
The proper C way of doing so is as follows
union
 {
  double dbv;
  struct { unsigned long lo, hi } ul;
 } D22UL;

D22UL var;
var.dbv = (double) 3.14159;
printf("%ld %ld", var.ul.lo, var.ul.hi);
0
 
LVL 1

Expert Comment

by:newexpert
Comment Utility
Just noticed xyu's answer.  Sorry xyu.  At least I used unsigned long as requested
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I wouldn't quite say it's the "proper" C way.
I don't think the ANSI C specifications actually require that to work any more than the pointer cast method.
(On the other hand, I'm not familiar with any implementation in which it wouldn't work, provided that sizeof(double)==2*sizeof(unsigned long))


halen's solution of casting a double to an unsigned long seems very unlikely to be what bsmoam1 was asking for.
0
 
LVL 1

Expert Comment

by:newexpert
Comment Utility
ozo:  There are non-portable features in ANSI compliant C such as
bit_field (which can never be portable).

In the future if a 64 bit machine requires every integer (long included) to be aligned on 64 bit boundary (just like some unix systems nowadays require all char and int to be on 32 bit boundary) then the above example using union would not work.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Even if the hardware does not  require such alignment, the C implementation is still permited to use such algnments in its unions.
0
 
LVL 1

Expert Comment

by:newexpert
Comment Utility
ozo: but not in struct {} !!

struct { char c; int d; } A;
struct { int d1; int d2; } B;

sizeof(A) == sizeof(B) on some machines.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Or on any machine, if the C impementation chooses to make it so.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now