Thread: bit-fields

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    3

    Question bit-fields

    if i define a structure as :

    struct
    {
    int a : 1 ;
    int b : 10 ;
    /* and blah..blah..blah*/
    } myStruct;

    and they say :

    myStruct.a = 1;

    now why wud the member 'a' get a value of -1 from this ?? i know the work around would be to use unsigned as the type....but what's the reason ?

    thanx,
    kps

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    That's because the most significant bit in an int represents the sign bit. So if you limit your int to 1 bit, your only possible values you can attain is 0 and -1. Use unsigned int if you want the values to be 0 and 1.

    ints have the following ranges:
    1-bit: -1 to 0
    2-bit: -2 to 1
    3-bit: -4 to 3
    etc...
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm.... just a side thought, could you make a 256-bit int using bit-fields?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    sure, i don't see why not
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Wow! And if I made a 1024-bit int, that would work too?

    That's a really big number!
    1.797693134862315907729305190789 e308
    Could I modify it like any other int too? (i.e. crazyint.value += 3238)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Only if you've overloaded +=, but that's a c++ thing. It's more complicated than what you've stated though. You'll probably have to write your own functions to add,sub,mult,div and also to print.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hunter2
    Hmm.... just a side thought, could you make a 256-bit int using bit-fields?
    No. Not in the true sense of the word anyway. Yeah, you could make an array of 256 bits, and use that, which is basicly what a bitfiled is. But no, you couldn't simply use standard operators on it. Furthermore, you'd have to create a whole set of functions to manipulate them.

    For example, using standard numbers, there is no way you could ever do this:

    myVar = myFun( 1234567890987654321 );

    The only way you'd be able to do that, would be to use strings. Just using "pure numbers" as it were, this is impossible, even if this was C++ and you created your own classes.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Fiddlesticks
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Hunter2
    Fiddlesticks
    there are big number math libraries for C available, out there somewheres on the intarweb.
    hello, internet!

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, I don't really need big numbers, so I guess I'll pass on that for now Maybe some other time.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. union in struct with bit fields
    By sagarnewase in forum C Programming
    Replies: 4
    Last Post: 05-12-2008, 07:30 AM
  2. Replies: 7
    Last Post: 12-10-2004, 08:18 AM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  4. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM