Thread: cross-architecture bitfields use

  1. #1
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528

    cross-architecture bitfields use

    Am trying to use bitfields in a portable way but am not sure my solution is the best one. I could be missing something.

    I am declaring the bitfields conditionally based on the target-architecture. I have something like

    Code:
    struct CMDInvalDevEntry {
    
    
    #ifdef __BIG_ENDIAN_BITFIELD
        uint16_t devid;                /* device to invalidate */
        uint64_t reserved_1:44;   
        uint32_t type:4;               /* command type         */
    #else
        uint16_t devid;                
        uint64_t reserved_1:44;
        uint32_t type:4;
    #endif /* __BIG_ENDIAN_BITFIELD */
    
    
        uint64_t reserved_2;
    } __attribute((packed));
    Is there a better way to do this ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,114
    Bitfields are only useful for storing internal data.

    The fact that you're trying to use "bitfields in a portable way" suggests you're trying to pack a structure of bitfields for communication with some external entity.
    Question 2.26

    There is no way for you to portably specify whether your 'devid' occupies the top 16 bits or the bottom 16 bits of the first 64bit storage unit.

    If you're trying to send bits to something outside the program, the best thing to do is write your own serialiser interface.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Salem View Post
    Bitfields are only useful for storing internal data.
    It's actually for internal storage but this is some really obscure software so I guess I will go with the above approach.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 08-06-2012, 12:34 AM
  2. BitFields -- is this possible?
    By trish in forum C Programming
    Replies: 6
    Last Post: 01-22-2012, 02:22 AM
  3. Cross platform plugin architecture
    By onetwoandthree in forum C++ Programming
    Replies: 5
    Last Post: 08-22-2010, 02:51 PM
  4. bitfields
    By Hunter2 in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2002, 05:21 PM

Tags for this Thread