offset macro for bit fields

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tehn Yit Chin

    offset macro for bit fields

    Does anyone know of an equivalent macro to offset() that would work
    for bit fields?

    eg,

    struct abc
    {
    enable: 1;
    disable: 1;
    speed: 6;
    };

    enable_bit_posi tion = offset_bit(stru ct abc, enable);


    Thanks in advance for any hints.
    Cheers,
  • santosh

    #2
    Re: offset macro for bit fields

    Tehn Yit Chin wrote:
    Does anyone know of an equivalent macro to offset() that would work
    for bit fields?
    >
    eg,
    >
    struct abc
    {
    enable: 1;
    disable: 1;
    speed: 6;
    };
    >
    enable_bit_posi tion = offset_bit(stru ct abc, enable);
    The address of bit-fields may not be taken. What is it that you actually
    want to accomplish? Perhaps there may be a way other than you imagine.

    Comment

    • Tehn Yit Chin

      #3
      Re: offset macro for bit fields

      On Jul 9, 6:24 pm, santosh <santosh....@gm ail.comwrote:
      Tehn Yit Chin wrote:
      Does anyone know of an equivalent macro to offset() that would work
      for bit fields?
      >
      eg,
      >
      struct abc
      {
      enable: 1;
      disable: 1;
      speed: 6;
      };
      >
      enable_bit_posi tion = offset_bit(stru ct abc, enable);
      >
      The address of bit-fields may not be taken. What is it that you actually
      want to accomplish? Perhaps there may be a way other than you imagine.
      I am doing some investigation on ways which the bit field could be
      modified.

      For example, given a more complex structure such as

      struct speed_tag
      {
      union
      {
      unsigned int speed;
      struct speed_part
      {
      unsigned int left_wheel:4;
      unsigned int right_wheel:4;
      }
      }
      }

      struct speed_tag my_car;

      /* access the member directly */
      my_car.speed_pa rt.left_wheel = current_speed;

      /* another way of accessing */
      my_car.speed &= 0x0f;
      my_car.speed |= current_speed << 4;

      /* what I am looking for is */
      my_car.speed &= 0x0f;
      my_car.speed |= current_speed << offset_bit(stru ct speed_part, speed);


      I realise that this technique is only applicable to the platform it is
      targeted for and, at this level, portability is not possible.

      Cheers,

      Comment

      • Tehn Yit Chin

        #4
        Re: offset macro for bit fields

        On Jul 10, 9:14 am, Tehn Yit Chin <tehn.yit.c...@ gmail.comwrote:
        On Jul 9, 6:24 pm, santosh <santosh....@gm ail.comwrote:
        >
        >
        >
        Tehn Yit Chin wrote:
        Does anyone know of an equivalent macro to offset() that would work
        for bit fields?
        >
        eg,
        >
        struct abc
        {
        enable: 1;
        disable: 1;
        speed: 6;
        };
        >
        enable_bit_posi tion = offset_bit(stru ct abc, enable);
        >
        The address of bit-fields may not be taken. What is it that you actually
        want to accomplish? Perhaps there may be a way other than you imagine.
        >
        I am doing some investigation on ways which the bit field could be
        modified.
        >
        For example, given a more complex structure such as
        >
        struct speed_tag
        {
        union
        {
        unsigned int speed;
        struct speed_part
        {
        unsigned int left_wheel:4;
        unsigned int right_wheel:4;
        }
        }
        >
        }
        >
        struct speed_tag my_car;
        >
        /* access the member directly */
        my_car.speed_pa rt.left_wheel = current_speed;
        >
        /* another way of accessing */
        my_car.speed &= 0x0f;
        my_car.speed |= current_speed << 4;
        >
        /* what I am looking for is */
        my_car.speed &= 0x0f;
        my_car.speed |= current_speed << offset_bit(stru ct speed_part, speed);
        >
        I realise that this technique is only applicable to the platform it is
        targeted for and, at this level, portability is not possible.
        >
        Cheers,
        FWIW, I just came across a patent on this.

        Comment

        • Ian Collins

          #5
          Re: offset macro for bit fields

          Tehn Yit Chin wrote:
          FWIW, I just came across a patent on this.
          http://www.freepatentsonline.com/6938241.html
          Who are they kidding?

          --
          Ian Collins.

          Comment

          • Ben Bacarisse

            #6
            Re: offset macro for bit fields

            Tehn Yit Chin <tehn.yit.chin@ gmail.comwrites :
            On Jul 9, 6:24 pm, santosh <santosh....@gm ail.comwrote:
            >Tehn Yit Chin wrote:
            Does anyone know of an equivalent macro to offset() that would work
            for bit fields?
            <snip>
            I am doing some investigation on ways which the bit field could be
            modified.
            >
            For example, given a more complex structure such as
            >
            struct speed_tag
            {
            union
            {
            unsigned int speed;
            struct speed_part
            {
            unsigned int left_wheel:4;
            unsigned int right_wheel:4;
            }
            }
            }
            >
            >
            struct speed_tag my_car;
            >
            /* access the member directly */
            my_car.speed_pa rt.left_wheel = current_speed;
            >
            /* another way of accessing */
            my_car.speed &= 0x0f;
            my_car.speed |= current_speed << 4;
            >
            /* what I am looking for is */
            my_car.speed &= 0x0f;
            my_car.speed |= current_speed << offset_bit(stru ct speed_part, speed);
            >
            >
            I realise that this technique is only applicable to the platform it is
            targeted for and, at this level, portability is not possible.
            It is not a good idea to combine these into a union. You say tou
            don't care about portability because you will use one platform, but
            the association can change between compiler on the same platform,
            between compiler versions of the same compiler and even between
            compiler options with the same compiler version.

            If you need named access, a bit field will do. If you need access by
            position (or some other computed data) you need to use the well-known
            shift and mask methods.

            --
            Ben.

            Comment

            • Richard Bos

              #7
              Re: offset macro for bit fields

              Ian Collins <ian-news@hotmail.co mwrote:
              Tehn Yit Chin wrote:
              >
              FWIW, I just came across a patent on this.
              http://www.freepatentsonline.com/6938241.html
              >
              Who are they kidding?
              The US patent office, apparently.

              Richard

              Comment

              • rahul

                #8
                Re: offset macro for bit fields

                On Jul 10, 4:26 am, Tehn Yit Chin <tehn.yit.c...@ gmail.comwrote:

                struct speed_tag
                {
                   union
                   {
                      unsigned int speed;
                      struct speed_part
                      {
                          unsigned int left_wheel:4;
                          unsigned int right_wheel:4;
                      }
                   }
                >
                }
                >
                my_car.speed_pa rt.left_wheel = current_speed;
                Does this snippet compile? speed_part declares the type. You have not
                declared a member named speed_part.

                Comment

                Working...