Bit Manipulation

Discussion in 'C Programming' started by Al, Mar 6, 2007.

  1. Al

    Al Guest

    HI,
    I'm an hardware designer that HAS to define the software structure for
    accessing "my" hardware, so please be patient with me.

    Let me put my problem first:
    I have "a lot" of "hardware" variables that are non-standard size
    (like one, two, seven and so on - bits). What is the best way to work
    with them? They MUST be "composed" into 32-bit words before writing
    them into the hardware.

    Now let me go a little bit more specific:
    My first hint was to use a structure of bit-field, but I have some
    questions that could not find an answer:
    1- Due to portability issues I CANNOT assume that a "group" of bit-
    fields summing 32-bits is stored in and int (unsigned)? (I'm correct?)
    2- Bit-fields can be viewed as a sub-set of int (?). What if my bit-
    field has more bits than the processor defined int? (Example: a bit-
    field with 40-bits in a 32-bit machine?) What happens in this
    situation?
    3- For grouping a set of bit-fields into and unsigned int can I still
    use bit-shifting?
    4- What happens if I try something like i=255 and I has only 4 bits?
    5- The alternatives for bit-field are using simple unsigned int and
    perform bit manipulation, but that complicates programming? (Do you
    agree?) (Note that 32-bit composing is limited to communication
    functions.)


    If you have other suggestions that you would like to share I would
    appreciate it Very Much.

    Thank you for your attention and patience.
     
    Al, Mar 6, 2007
    #1
    1. Advertising

  2. Al

    santosh Guest

    Al wrote:
    > HI,
    > I'm an hardware designer that HAS to define the software structure for
    > accessing "my" hardware, so please be patient with me.


    <snip>

    Please try comp.arch
     
    santosh, Mar 6, 2007
    #2
    1. Advertising

  3. Al

    Jason Curl Guest

    Al wrote:
    > HI,
    > I'm an hardware designer that HAS to define the software structure for
    > accessing "my" hardware, so please be patient with me.
    >
    > Let me put my problem first:
    > I have "a lot" of "hardware" variables that are non-standard size
    > (like one, two, seven and so on - bits). What is the best way to work
    > with them? They MUST be "composed" into 32-bit words before writing
    > them into the hardware.
    >
    > Now let me go a little bit more specific:
    > My first hint was to use a structure of bit-field, but I have some
    > questions that could not find an answer:
    > 1- Due to portability issues I CANNOT assume that a "group" of bit-
    > fields summing 32-bits is stored in and int (unsigned)? (I'm correct?)
    > 2- Bit-fields can be viewed as a sub-set of int (?). What if my bit-
    > field has more bits than the processor defined int? (Example: a bit-
    > field with 40-bits in a 32-bit machine?) What happens in this
    > situation?
    > 3- For grouping a set of bit-fields into and unsigned int can I still
    > use bit-shifting?
    > 4- What happens if I try something like i=255 and I has only 4 bits?
    > 5- The alternatives for bit-field are using simple unsigned int and
    > perform bit manipulation, but that complicates programming? (Do you
    > agree?) (Note that 32-bit composing is limited to communication
    > functions.)
    >
    >
    > If you have other suggestions that you would like to share I would
    > appreciate it Very Much.


    You also have to be careful, that if you're using C, the ordering of the
    bitfields is implementation defined. We had this problem that by
    changing compilers we had to redefine all our structures to keep the
    same bit representation.

    Why not define an unsigned int (or if using Posix a uint32_t defined by
    <inttypes.h>) and define macros to set and clear bits? To keep
    portability if you need it (but there'll be a performance cost) don't
    use bitfields in a structure but define an API/wrapper that will take a
    portable structure and convert it to the bit field you want just before
    you write directly to the hardware?

    >
    > Thank you for your attention and patience.
    >
     
    Jason Curl, Mar 6, 2007
    #3
    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. Replies:
    8
    Views:
    34,399
    Luc The Perverse
    Nov 26, 2005
  2. Chris \( Val \)

    Re: Bit manipulation

    Chris \( Val \), Jun 26, 2003, in forum: C++
    Replies:
    0
    Views:
    837
    Chris \( Val \)
    Jun 26, 2003
  3. Replies:
    3
    Views:
    1,850
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    1,048
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    202
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page