https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125817

--- Comment #2 from Alejandro Colomar <[email protected]> ---
(In reply to Drea Pinski from comment #1)
> This union:
> 
> union ln {
>       unsigned _BitInt(256)  ln;
>       unsigned _BitInt(1)    e[256];
> };
> 
> Does not do what you think it does.
> 
> `unsigned _BitInt(1)` has 7bit padding.
> So when you do:
> 
>       l.e[5] = true;
>       l.e[4] = true;
>       l.e[2] = true;
>       l.e[7] = true;
>       l.e[20] = true;
>       l.e[30] = true;
>       l.e[60] = true;
>       l.e[77] = true;
> 
> It does not set bit 6, 4,2, etc. but rather 5*8, 4*8, 2*8, etc. bit of the
> whole union.

Ouch!  Indeed, you're right.

alx@devuan:~/tmp$ cat bar.c 
union ln {
        unsigned _BitInt(64)  ln;
        unsigned _BitInt(1)   e[64];
};

int
main(void)
{
        return sizeof(union ln);
}
alx@devuan:~/tmp$ gcc -Wall -Wextra bar.c 
alx@devuan:~/tmp$ ./a.out; echo $?
64


I was hoping this would be a way of doing bit-mask work without having to do
explicit bit masking, similar to how bitfields work, but with array notation.

Would it be possible to remove that padding somehow?  This would be quite nice.

Reply via email to