Bitwise Operation

Discussion in 'C Programming' started by Magix, Oct 15, 2004.

  1. Magix

    Magix Guest

    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);
     
    Magix, Oct 15, 2004
    #1
    1. Advertising

  2. Magix

    Pedro Graca Guest

    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
     
    Pedro Graca, Oct 15, 2004
    #2
    1. Advertising

  3. Magix

    Michael Mair Guest

    Hi Magix,

    Magix wrote:
    > 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
     
    Michael Mair, Oct 15, 2004
    #3
  4. 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-
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Oct 15, 2004
    #4
  5. Magix

    Dimension Guest

    On Fri, 15 Oct 2004 15:12:43 +0200, Nils O. SelÄsdal <> 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, Oct 15, 2004
    #5
  6. Magix

    Dimension Guest

    On 15 Oct 2004 11:47:54 GMT, Pedro Graca <> 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/
     
    Dimension, Oct 15, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pasquale Imbemba

    Bitwise Operation

    Pasquale Imbemba, May 6, 2004, in forum: Java
    Replies:
    2
    Views:
    558
    Roedy Green
    May 7, 2004
  2. biswaranjan.rath

    bitwise AND operation in xslt

    biswaranjan.rath, May 8, 2006, in forum: XML
    Replies:
    3
    Views:
    4,934
    shaunroe
    Nov 12, 2008
  3. Patrick Hoonhout

    bitwise operation...

    Patrick Hoonhout, Aug 27, 2003, in forum: C Programming
    Replies:
    13
    Views:
    681
    Alan Balmer
    Aug 28, 2003
  4. Magix

    Bitwise operation

    Magix, Jul 30, 2004, in forum: C Programming
    Replies:
    6
    Views:
    748
    Joe Wright
    Jul 31, 2004
  5. Jerry

    Bitwise operation for division

    Jerry, Mar 2, 2005, in forum: C Programming
    Replies:
    7
    Views:
    8,280
    Chris Williams
    Mar 2, 2005
Loading...

Share This Page