Thread: Passing Bit Fields

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    35

    Passing Bit Fields

    Is there a way to return bit fields via class methods? ..Or even typedef them?...Or maybe even (void) pointers to them?

    example:
    Code:
     
    class BitField
    {
       typedef unsigned:28  Bytes7
       public:
       unsigned:28 getBits(){ return bits; }
       unsigned bits:28;
    };
    If nothing else, a native data type 28 bits or larger?
    Last edited by computation; 09-26-2006 at 06:28 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ask yourself "unsigned what?". Return that.


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

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    35
    but int's aren't that big

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Have you considered STL's bitset<N> as a solution?

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by computation
    but int's aren't that big
    A long is guaranteed by the standard to be at least 32 bits.

    Int, in general, will be 32 bits on a 32 bit compiler, and 64 bits on a 64 bit compiler.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Since you can't take the address of bit fields, you can't return a pointer to them. You have to return an instance of the whole enclosing structure or the value of it via an unsigned/signed integer. (Bit fields can only be of type unsigned/unsigned int or signed/int/signed int.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit fields
    By Perspektyva in forum C++ Programming
    Replies: 13
    Last Post: 11-22-2008, 02:38 PM
  2. 64 bit testing
    By DrSnuggles in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2007, 03:20 AM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  4. Create data type
    By Malefaust in forum C Programming
    Replies: 2
    Last Post: 09-30-2004, 06:06 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM