Bit manipulation

Discussion in 'C Programming' started by yezi, Nov 22, 2005.

  1. yezi

    yezi Guest

    Hi:
    One question about the bit manipulation: if the code is like :

    int sequence:18;

    is that mean, in memory, although generally for int, it occupy 32 bits,
    but now by define 18, which means the real occupancy is 18 bits?

    Is that understanding right?

    Thanks for any comments.


    bin YE
    yezi, Nov 22, 2005
    #1
    1. Advertising

  2. yezi

    Kwan Lim Guest

    I assume you mean

    int sequence = 18;

    the size of the integer will be machine dependant but is typically 2 or
    4 bytes.
    What happens in that assignment statment is that the binary 2's
    complement bit pattern for 18 is stored in the memory of the variable.
    It is this pattern that will take up the bits.
    Kwan Lim, Nov 22, 2005
    #2
    1. Advertising

  3. yezi

    yezi Guest

    The sentence is

    int sequence :18, not

    int sequence =18.
    yezi, Nov 22, 2005
    #3
  4. yezi

    Michael Mair Guest

    yezi wrote:
    > Hi:
    > One question about the bit manipulation: if the code is like :


    Do you mean _bitfields_? If yes, please say so.


    > int sequence:18;
    >
    > is that mean, in memory, although generally for int, it occupy 32 bits,
    > but now by define 18, which means the real occupancy is 18 bits?
    >
    > Is that understanding right?


    Note that bitfields occur only as structure members.
    If you define an unsigned int bitfield of width N then
    you know that you are really working modulo pow(2,N).
    However, that you cannot get the address of a bitfield
    nor know the internal representation or even the order
    of consecutive bitfields.

    If you have
    struct example {
    int foo;
    int bar:5;
    int baz;
    } qux;
    then baz will still start at an address with alignment
    proper to int and the number of bits between the "end"
    of qux.foo and the start of qux.baz will be more than
    five.


    As an aside: int is guaranteed to have at least 16 bits,
    nothing more.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Nov 22, 2005
    #4
  5. yezi

    yezi Guest

    then, which means if the memory is continuous, the memory should like
    the following :

    16 bits for qux.foo;
    5 bits for qux.bar;
    3 bits for anystuff which is not belong to the structure;
    16 bits for qux.baz;

    Is that understanding right?


    Thanks
    yezi, Nov 22, 2005
    #5
  6. yezi

    gooch Guest

    yezi wrote:
    > then, which means if the memory is continuous, the memory should like
    > the following :
    >
    > 16 bits for qux.foo;
    > 5 bits for qux.bar;
    > 3 bits for anystuff which is not belong to the structure;
    > 16 bits for qux.baz;
    >
    > Is that understanding right?
    >
    >
    > Thanks


    What it looks like in memory depends on the implementation and how many
    bits it uses to represent an int as well as how it aligns structure
    elements. Where the bits in the bit field are may also depend on the
    endianess of the platform.
    gooch, Nov 23, 2005
    #6
  7. "yezi" <> writes:

    > then, which means if the memory is continuous, the memory should like
    > the following :


    [snipped by yezi and reincluded by me]

    struct example {
    int foo;
    int bar:5;
    int baz;
    } qux;

    > 16 bits for qux.foo;


    No. sizeof(int) * CHAR_BIT for qux.foo

    Followed by an unspecifed amount of padding bits, followed by

    > 5 bits for qux.bar;


    > 3 bits for anystuff which is not belong to the structure;


    No, not necessary 3 bit, but another unspecifed amount of padding bits,
    which will result in correct alignment for the next struct member.

    > 16 bits for qux.baz;


    No, sizeof(int) * CHAR_BIT here too.

    > Is that understanding right?


    No.

    CHAR_BIT is at least 8, and 8 is the most common value in todays
    implementations, but other values do exist. There are systems with
    9 bit characters, and 36 bit integers for example.

    sizeof(int) must be at least 2 on implementations where CHAR_BIT is 8,
    but more common today are 4, and 8 is not unheard of.

    An implementation is allowed to insert padding (unused memory space)
    between members in a struct, so that all members will be properly
    aligned. The amount of padding is implementation specific, and may
    vary between platform, between compilers, between compiler versions,
    and possibly depending on compiler settings. In practice it can be
    a good idea to put the larger sized members first in a struct, and
    to have same sized members adjacent to each other, to minimize the
    amount of padding.

    /Niklas Norrthon
    Niklas Norrthon, Nov 23, 2005
    #7
    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,288
    Luc The Perverse
    Nov 26, 2005
  2. Chris \( Val \)

    Re: Bit manipulation

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

Share This Page