Are there any C implementations that have a useless one-bit bitfield `int`?

6.7.2.1p9 from n1570 says:

A member of a structure or union can be of any complete object type other than the modified type .123). In addition, a member can be declared to consist of a specified number of bits (including the sign bit, if any). Such a member is called a bit-field; 124), its width is preceded by a colon.

Do I understand correctly that this means that the only member in struct { int bit:1; }

can be the sign bit?

If this is the case, then it follows that the only values โ€‹โ€‹that a bitfield may have for some implementations are 0

and -0

, of which -0

may be indistinguishable from 0

post-save or trap.

Are there any actual implementations where only one value can be assigned to such a bitfield?

+3


source to share


2 answers


How about gcc 4.9.2?

/* gcc -std=c11 -pedantic-errors -Wextra -Werror=all test.c */
#include <stdio.h>
#include <string.h>

struct foo {
    int bit:1;
};

int main() {
    struct foo f;
    f.bit = 0;
    f.bit = 1;

    printf("%i\n", f.bit);
    return 0;
}

      

Compilation:



$ gcc -std=c11 -pedantic-errors -Wextra -Werror=all test.c
test.c: In function โ€˜mainโ€™: test.c:12:10: warning: overflow in
implicit constant conversion [-Woverflow]
  f.bit = 1;
          ^

      

Running it emits:

$ ./a.out 
-1

      

+5


source


In 2's complement, all-bit-1 is the maximum negative value. Thus, the sign bit 0

represents 0

and the sign bit 1

represents -1

.



You are describing the representation of a sign value in your question, in which case the values โ€‹โ€‹represented will indeed be +0

and -0

. But I don't know of any C ++ implementation that didn't use 2's padding.

+3


source







All Articles