Thread: Bit fields, small data types and setting bits

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    112

    Bit fields, small data types and setting bits

    I have struct that I am populating, and I'm trying to figure out what is the best way to utilize them.

    Some of the data types are floats, bools, and unsigned ints. The unsigned ints range from say 2 bits to 10 bits being used. Do people normally store each one in the smallest possible data type that holds the data, such as char, short, etc.?

    I've seen other instances where bit fields are used to somewhat keep the size of the struct down. If you use int i:3, does that only allow values with 3 bits to be stored in the variable? I know this only works to some extent. If you have an OS that uses 4 byte ints, and you used 33 bit fields it would say the data in 2 ints.

    The other method is to store the data in an int with bit shifting and bit masks. This is probably the best approach to keep memory usage down, and may lose some flexibility with the interface if the size of variables change.

    I'd like to know what people normally do, and I think bit fields are pretty easy and convenient, but I could be way off.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by CodeSlapper View Post
    I have struct that I am populating, and I'm trying to figure out what is the best way to utilize them.

    Some of the data types are floats, bools, and unsigned ints. The unsigned ints range from say 2 bits to 10 bits being used. Do people normally store each one in the smallest possible data type that holds the data, such as char, short, etc.?

    I've seen other instances where bit fields are used to somewhat keep the size of the struct down. If you use int i:3, does that only allow values with 3 bits to be stored in the variable? I know this only works to some extent. If you have an OS that uses 4 byte ints, and you used 33 bit fields it would say the data in 2 ints.

    The other method is to store the data in an int with bit shifting and bit masks. This is probably the best approach to keep memory usage down, and may lose some flexibility with the interface if the size of variables change.

    I'd like to know what people normally do, and I think bit fields are pretty easy and convenient, but I could be way off.
    As long as you don't have to worry about the underlying memory layout (e.g.: you don't implement your own networking protocol), or any memory footprint, there is no need to worry about bits. Just use the type which you will be most comfortable with.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    When I use bitfields I get warning of the struct being packed with bits or bytes to align everything. Should I worry about it? Or is the compiler letting me know I'm being inefficient?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by CodeSlapper View Post
    When I use bitfields I get warning of the struct being packed with bits or bytes to align everything. Should I worry about it? Or is the compiler letting me know I'm being inefficient?
    You have to show the actual code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting bits in a byte
    By andyessex40 in forum C Programming
    Replies: 2
    Last Post: 07-13-2010, 01:16 PM
  2. setting the values of bits
    By yahn in forum C++ Programming
    Replies: 14
    Last Post: 04-14-2008, 12:09 AM
  3. Number of bits in types
    By TriKri in forum C Programming
    Replies: 7
    Last Post: 12-02-2006, 12:56 PM
  4. Setting bits
    By Buckshot in forum C++ Programming
    Replies: 15
    Last Post: 06-06-2005, 12:37 PM
  5. Setting bits
    By Kinasz in forum C Programming
    Replies: 9
    Last Post: 07-05-2004, 05:12 AM