Bitwise Operation

M

Magix

Hi,

Can I do this?
long mantissa; long x1; long x2;
word a1; word a2;
int exp;
double result;

a1= 0x1234
a2 = 0x5678

x1 = (a1 << 16)
x2 =(a2 & 0x0000FFFF)
mantissa= x1 | x2

so that I will have 0x56781234

result = ldexp(mantissa, exp);
 
P

Pedro Graca

Magix said:
Hi,

Can I do this?

I expect this is inside some function ???

void some_function(void) {
long mantissa; long x1; long x2;
word a1; word a2;

'word' is not a valid C type.
int exp;
double result;

a1= 0x1234
a2 = 0x5678

You need semicolons here
x1 = (a1 << 16)
x2 =(a2 & 0x0000FFFF)
mantissa= x1 | x2

semicolons here too
so that I will have 0x56781234

What hapenned when you tried?
I get mantissa == 0x12345678
result = ldexp(mantissa, exp);

UB: exp is undefined.


and you need to #include <math.h> for ldexp
 
M

Michael Mair

Hi Magix,
Hi,

Can I do this?

Of course you can, but to no avail.

I already asked you in other threads and ask you one last time:
Please give us a piece of code that runs.
Either a minimal example or whatever.
Your code below does not work and cannot work.
Tell us what you want to achieve.

long mantissa; long x1; long x2;
word a1; word a2;
word is not a C type.
If you want to work with bitwise operations, it is
a Good Idea to use unsigned integer types.
Use
unsigned long mantissa, x1, x2;
unsigned int a1, a2;
int exp;
double result;

a1= 0x1234
a2 = 0x5678
This lacks semicolons.
x1 = (a1 << 16)
x2 =(a2 & 0x0000FFFF)
mantissa= x1 | x2

so that I will have 0x56781234
Not at all.
x1 is a1 shifted left by 16, i.e. 0x12340000, so
x1|x2 is 0x12345678.
result = ldexp(mantissa, exp);

From the ldexp manpage on my system:
"
ldexp - multiply floating-point number by integral power of 2

SYNOPSIS
#include <math.h>

double ldexp(double x, int exp);

DESCRIPTION
The ldexp() function returns the result of multiplying the
floating-point number x by 2 raised to the power exp.

CONFORMING TO
SVID 3, POSIX, BSD 4.3, ISO 9899
"

I guess that you want to do something along the lines of
setting the mantissa bits of a double variable with bitwise
operations and then multiply it by pow(2,exp).
This cannot be done portably.
Rather get yourself a number in the range 0<=x<1 and then
scale it with ldexp.

If my guess is wrong: Call mantissa differently to avoid
the impression that it should be a double.


--Michael
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Magix said:
Hi,

Can I do this?
long mantissa; long x1; long x2;
word a1; word a2;
int exp;
double result;

a1= 0x1234
a2 = 0x5678

x1 = (a1 << 16)
x2 =(a2 & 0x0000FFFF)
the last one here is probably not needed...
use unsigned types btw, so tou don't get to many
surprises.
mantissa= x1 | x2

so that I will have 0x56781234
Yes, assuming that long and "word" on your
system are able to hold enough bits for this,
and that the above is more or less pseudocode.
result = ldexp(mantissa, exp);
Uhmm, that line is totally out of context, what's
the meaning of this-
 
D

Dimension

the last one here is probably not needed...
use unsigned types btw, so tou don't get to many
surprises.

Why the last one x2=(a2 & 0x0000FFFF) not needed?
if I have a1=0x1234, a2=0x5678, mantissa must be 0x56781234. To construct
that, I only need to left shift a2 16? and then x1|x2 ?
Yes, assuming that long and "word" on your
system are able to hold enough bits for this,
and that the above is more or less pseudocode.

Actually,i want to put mantissa and exp into ldexp (math.h) function.
and the result should be xxx.xxxxxx (%5lf). So probably define
unsigned double result?
 
D

Dimension

I expect this is inside some function ???

of coz it is inside a function. but anyway, this is not important.
void some_function(void) {


'word' is not a valid C type.


You need semicolons here

of coz I know semicolon is needed. I'm not asking to check semicolon.
I'm asking if I have a1=0x1234, a2=0x5678, how can I combine both to be
0x56781234 (4bytes) and asisgned to "mantissa" variable.

Then I will do the ldexp to get the value in double format.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top