0

Running the below code give me this result: 01010101.

Does this mean the machine places these bits from right to left? Is there any way to tell it to reverse the order? i.e the first bit in the struct (bit7) is the most significant bit. Do all machines work the same way? In another word, is the code portable?

#include <stdio.h>
#include <string.h>
#include <stdint.h>
struct dummyStruct_t
{
    uint8_t bit7 :1;
    uint8_t bit6 :1;
    uint8_t bit5 :1;
    uint8_t bit4 :1;
    uint8_t bit3 :1;
    uint8_t bit2 :1;
    uint8_t bit1 :1;
    uint8_t bit0 :1;
};

struct dummyStruct_t dummyStruct;

int main() {

    uint8_t value = 0xAA;
    memcpy(&dummyStruct, &value, sizeof(uint8_t));
    
    printf("%d", dummyStruct.bit7);
    printf("%d", dummyStruct.bit6);
    printf("%d", dummyStruct.bit5);
    printf("%d", dummyStruct.bit4);
    printf("%d", dummyStruct.bit3);
    printf("%d", dummyStruct.bit2);
    printf("%d", dummyStruct.bit1);
    printf("%d", dummyStruct.bit0);
    
    return 0;
}
HV16
  • 25
  • 5
  • 2
    The way that bit fields are ordered is implementation-dependent. – Barmar Aug 16 '23 at 21:13
  • 2
    If you want to set specific bits in a number, use shifting and masking, not bit fields. – Barmar Aug 16 '23 at 21:14
  • @Barmar but I cannot do shifting and masking for elements in a struct, or can I? – HV16 Aug 16 '23 at 21:17
  • What result do you expect from shifting a 1bit value? – Yunnosch Aug 16 '23 at 21:18
  • @Yunnosch could you elaborate? I dont think I understand your question. – HV16 Aug 16 '23 at 21:24
  • You ask about "shifting and masking for elements in a struct". The elements of your struct are 1bit. So, assuming you can, what result would you expect from ? `dummyStrcut.bit1 <<=1;`. I ask indirectly to find out what you are trying to do. – Yunnosch Aug 16 '23 at 21:26
  • @HV16, you cannot do shifting or masking on a struct itself. But you can do all the shifting and masking you want on any struct *member* that has appropriate type for it. To be clear, however, that will not observe or affect the values of other members. – John Bollinger Aug 16 '23 at 21:27
  • As I said, you shouldn't use bit fields in the first place. Use `struct dummyStruct_t { uint8_t bits; }` and then use `dummyStruct.bits >> 6 & 0x1` to get the most significant bit. – Barmar Aug 16 '23 at 22:40
  • @Yunnosch that is the reason why I said it is not possible to shift the elements in this struct. – HV16 Aug 16 '23 at 22:59
  • @Barmar I am receiving a buffer full of these 1-bit values. This code was generated for verification as I notice the bits weren't assigned to the members that I expected in the original code. The idea was to create this struct and use memcpy to load the buffer data onto this struct; and access each bit using the struct.member. How about I re-arrange the order of the struct so that it matches the expectation? Is it reliable to do that? – HV16 Aug 16 '23 at 23:05
  • You can do whatever you want. The language doesn't make any guarantees, but if you happen to know the order that your implementation uses, you can write code that depends on it. – Barmar Aug 16 '23 at 23:07

0 Answers0