Velocity Reviews > Bitwise Operation

# Bitwise Operation

Magix
Guest
Posts: n/a

 10-15-2004
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);

Pedro Graca
Guest
Posts: n/a

 10-15-2004
Magix wrote:
> 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

--
USENET would be a better place if everybody read:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html

Michael Mair
Guest
Posts: n/a

 10-15-2004
Hi Magix,

Magix wrote:
> Hi,
>
> Can I do this?

Of course you can, but to no avail.

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?=
Guest
Posts: n/a

 10-15-2004
Magix wrote:
> 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-

Dimension
Guest
Posts: n/a

 10-15-2004
On Fri, 15 Oct 2004 15:12:43 +0200, Nils O. Selåsdal <(E-Mail Removed)> wrote:

> Magix wrote:
>> 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.
>

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 ?

>> 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.
>

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?

>> result = ldexp(mantissa, exp);

> Uhmm, that line is totally out of context, what's
> the meaning of this-

--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

Dimension
Guest
Posts: n/a

 10-15-2004
On 15 Oct 2004 11:47:54 GMT, Pedro Graca <(E-Mail Removed)> wrote:

> Magix wrote:
>> Hi,
>>
>> Can I do this?

>
> I expect this is inside some function ???
>

of coz it is inside a function. but anyway, this is not important.

> 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

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.

>
>> 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
>
>

--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/