BItwise Operation ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • loudking

    #1

    BItwise Operation ...

    Hello, all.

    I am not good at bitwise operation, so I wonder if some experts could
    do me a help.

    In a.c, somebody defined
    =============== =============== =============== ===
    # define u_int64 unsigned long long
    # define u_short unsigned short
    # define u_int unsigned int

    u_short txid_slt;
    u_int txid_sqn;

    u_int64 tx_id= ((u_int64)txid_ slt << 32) | (u_int64)txid_s qn;

    Later, in b.c, I have to reassemble tx_id into "slt" and "sqn".
    =============== =============== =============== ====
    struct redo_xid {
    u_int sqn;
    u_short slot;
    };

    Can I use the following expression to achieve this goal?
    =============== =============== =============== =====
    struct redo_xid xid = (struct redo_xid)tx_id;

    Many many thanks!

  • Nick Keighley

    #2
    Re: BItwise Operation ...

    On 30 Oct, 09:49, loudking <loudk...@gmail .comwrote:
    I am not good at bitwise operation,
    they aren't that complicated...

    so I wonder if some experts could
    do me a help.
    >
    In a.c, somebody defined
    =============== =============== =============== ===
    # define u_int64 unsigned long long
    long long doesn't have to be 64-bits

    # define u_short unsigned short
    # define u_int unsigned int
    you appear to be assuming int is 32 bits. It doesn't have to be.
    I generally don't like typedefs like this.

    u_short txid_slt;
    u_int txid_sqn;
    >
    u_int64 tx_id= ((u_int64)txid_ slt << 32) | (u_int64)txid_s qn;
    I wouldn't bother with the casts.

    u_int64 tx_id = (txid_slt << 32) | txid_sqn;

    Later, in b.c, I have to reassemble tx_id into "slt" and "sqn".
    =============== =============== =============== ====
    struct redo_xid {
    u_int sqn;
    u_short slot;
    >
    };
    >
    Can I use the following expression to achieve this goal?
    =============== =============== =============== =====
    struct redo_xid xid = (struct redo_xid)tx_id;
    why not reverse the previous step?

    struct redo_xid xid;

    xid.sqn = tx_id & 0xffffffff; /* 32 1s */
    xid.slot = (tx_id >32) & 0xffff;


    --
    Nick Keighley

    Casting is almost always wrong,
    and the places where it's right are rarely the places you'd guess.
    Richard Heathfield

    Comment

    • loudking

      #3
      Re: BItwise Operation ...

      why not reverse the previous step?
      >
      struct redo_xid xid;
      >
      xid.sqn = tx_id & 0xffffffff; /* 32 1s */
      xid.slot = (tx_id >32) & 0xffff;
      >
      --
      Thanks!

      Comment

      • Martin Wells

        #4
        Re: BItwise Operation ...

        loudking:
        # define u_int64 unsigned long long
        # define u_short unsigned short
        # define u_int unsigned int

        C has typedef for a reason:

        typedef long long unsigned u_int64;
        typedef short unsigned u_short;
        typedef unsigned u_int;

        The syntax for typedef is the same for defining a variable:

        int (*x)[7]; /* x is a variable */
        typedef int (*x)[7]; /* x is a type */


        You'll notice the difference when you try:

        typedef int *Type1;
        #define Type2 int*

        int main(void)
        {
        const Type1 a;
        const Type2 b;

        b = 0;

        return 0;
        }


        u_short txid_slt;
        u_int txid_sqn;
        >
        u_int64 tx_id= ((u_int64)txid_ slt << 32) | (u_int64)txid_s qn;
        >
        Later, in b.c, I have to reassemble tx_id into "slt" and "sqn".
        =============== =============== =============== ====
        struct redo_xid {
        u_int sqn;
        u_short slot;
        >
        };
        >
        Can I use the following expression to achieve this goal?
        =============== =============== =============== =====
        struct redo_xid xid = (struct redo_xid)tx_id;

        None of the C Standards say that you can... in fact they explicitly
        allow an implementation to explode in your face if you try. As for
        *your own* platform... who knows...

        Martin

        Comment

        • Keith Thompson

          #5
          Re: BItwise Operation ...

          Martin Wells <warint@eircom. netwrites:
          loudking:
          ># define u_int64 unsigned long long
          ># define u_short unsigned short
          ># define u_int unsigned int
          >
          C has typedef for a reason:
          >
          typedef long long unsigned u_int64;
          typedef short unsigned u_short;
          typedef unsigned u_int;
          [...]

          Agreed. C also has "unsigned short" and "unsigned int" for a reason.
          Just refer to "unsigned short" and "unsigned int" directly. The
          aliases "u_short" and "u_int" add nothing useful; they just make me
          wonder why you bothered to define new names for types that already
          have perfectly good names -- or, worse, whether you've defined
          "u_short" as something other than "unsigned short".

          As for u_int64, unsigned long long is required to be *at least* 64
          bits, but it's allowed to be wider.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          Working...