Solved

bitwise operation on double

Posted on 1998-07-28
18
241 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
ID: 1251821
Edited text of question
0
 
LVL 84

Expert Comment

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

Expert Comment

by:snoegler
ID: 1251823
ozo why didn't you post this as an answer? it is absolutely perfect.
0
Technology Partners: 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!

 
LVL 84

Expert Comment

by:ozo
ID: 1251824
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
ID: 1251825
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
ID: 1251826
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
ID: 1251827
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
ID: 1251828
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
ID: 1251829
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
 
LVL 6

Expert Comment

by:snoegler
ID: 1251830
No comment :)
0
 
LVL 3

Expert Comment

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

:)

0
 
LVL 1

Expert Comment

by:newexpert
ID: 1251832
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
ID: 1251833
Just noticed xyu's answer.  Sorry xyu.  At least I used unsigned long as requested
0
 
LVL 84

Expert Comment

by:ozo
ID: 1251834
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
ID: 1251835
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
ID: 1251836
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
ID: 1251837
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
ID: 1251838
Or on any machine, if the C impementation chooses to make it so.
0

Featured Post

Technology Partners: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

733 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