# Re: signed single-bit field

Discussion in 'C Programming' started by Lew Pitcher, Nov 29, 2011.

1. ### Lew PitcherGuest

On November 29, 2011 14:10, in comp.lang.c, wrote:

> Greetings,
>
> Curious about a signed, single-bit bitfield on a 1's complement machine.
> What are the possible values?
>
> On 2's complement, bit clear = 0, bit set = -1
>
> I assume on 1's complement bit clear = 0, bit set = -0
>
> but I cannot find that in the C99 standard.

I'm not certain that the standard actually addresses 1-bit signed bitfields
in a consistent manner.

In 6.2.6.2 (Integer types), the standard says that a signed integer type
will have a single sign bit, some optional padding, and value bits. It is
emphatic that the padding is optional, and emphatic that the sign bit is
required, but I cannot tell if the value bits are /required by the
standard/ or not.
6.2.6.2 Integer types
...
2 For signed integer types, the bits of the object representation shall be
divided into three groups: value bits, padding bits, and the sign bit.
There need not be any padding bits; there shall be exactly one signbit.

A bitfield is considered as an integer type, and a signed bitfield is a
signed integer type. Thus, a signed bitfield (of any length) /must/ have a
sign bit. It is not clear to me whether a signed bitfield is required to
carry any value bits.

A single-bit signed bitfield obviously carries a sign bit, and that sign bit
indicates a positive value (when 0) or a negative value (when 1). But,
since that sign bit is the only bit in a single-bit signed bitfield, there
is obviously no room for value bits. So, one interpretation of the C99
standard is that single-bit signed bitfields can not have a value; they

Another interpretation might be that the value of a single-bit signed
bitfield is not defined by the standard, and may be illegal. For that
matter, such a field may only carry trap representations, as 6.2.6.2 sub 2
suggests
Which of these (sign/value combinations) applies is implementation-deï¬ned,
as is whether the value with sign bit 1 and all value bits zero (for the
> =========================================================

ï¬rst two (sign/magnitude and two's complement), or with sign bit and all
> =========================

value bits 1 (for oneâ€™s complement), is a trap representationor a normal
> ============ ========================

value.

Perhaps this is a question best asked in the comp.std.c arena

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------

Lew Pitcher, Nov 29, 2011

2. ### James KuyperGuest

On 11/29/2011 02:55 PM, Lew Pitcher wrote:
....
> In 6.2.6.2 (Integer types), the standard says that a signed integer type
> will have a single sign bit, some optional padding, and value bits. It is
> emphatic that the padding is optional, and emphatic that the sign bit is
> required, but I cannot tell if the value bits are /required by the
> standard/ or not.

If it had only specified that padding was optional, the rest of the
sentence would clearly have implied that value bits are mandatory. If it
had only specified that sign bits are mandatory, that would clearly have
implied that value bits are optional. Specifying both leaves it unclear
whether having at least one value bit is mandatory or optional.

> A single-bit signed bitfield obviously carries a sign bit, and that sign bit
> indicates a positive value (when 0) or a negative value (when 1). But,
> since that sign bit is the only bit in a single-bit signed bitfield, there
> is obviously no room for value bits. So, one interpretation of the C99
> standard is that single-bit signed bitfields can not have a value; they

However, if signed single-bit bit-fields are in fact permitted, then
6.2.6.2p2 is very clear about the meaning of the sign bit, and what it
says is trivially applicable to such bit-fields.

James Kuyper, Nov 29, 2011

3. ### Tim RentschGuest

Lew Pitcher <> writes:

> On November 29, 2011 14:10, in comp.lang.c, wrote:
>
>> Greetings,
>>
>> Curious about a signed, single-bit bitfield on a 1's complement machine.
>> What are the possible values?
>>
>> On 2's complement, bit clear = 0, bit set = -1
>>
>> I assume on 1's complement bit clear = 0, bit set = -0
>>
>> but I cannot find that in the C99 standard.

>
> I'm not certain that the standard actually addresses 1-bit signed bitfields
> in a consistent manner.
>
> In 6.2.6.2 (Integer types), the standard says that a signed integer type
> will have a single sign bit, some optional padding, and value bits. It is
> emphatic that the padding is optional, and emphatic that the sign bit is
> required, but I cannot tell if the value bits are /required by the
> standard/ or not.
> 6.2.6.2 Integer types
> ...
> 2 For signed integer types, the bits of the object representation shall be
> divided into three groups: value bits, padding bits, and the sign bit.
> There need not be any padding bits; there shall be exactly one sign bit.
>
> A bitfield is considered as an integer type, and a signed bitfield is a
> signed integer type. Thus, a signed bitfield (of any length) /must/ have a
> sign bit. It is not clear to me whether a signed bitfield is required to
> carry any value bits.
>
> A single-bit signed bitfield obviously carries a sign bit, and that sign bit
> indicates a positive value (when 0) or a negative value (when 1). But,
> since that sign bit is the only bit in a single-bit signed bitfield, there
> is obviously no room for value bits. So, one interpretation of the C99
> standard is that single-bit signed bitfields can not have a value; they
>
> Another interpretation might be that the value of a single-bit signed
> bitfield is not defined by the standard, and may be illegal. For that
> matter, such a field may only carry trap representations, as 6.2.6.2 sub 2
> suggests
> Which of these (sign/value combinations) applies is implementation-defined,
> as is whether the value with sign bit 1 and all value bits zero (for the
>> =========================================================

> first two (sign/magnitude and two's complement), or with sign bit and all
>> =========================

> value bits 1 (for one's complement), is a trap representation or a normal
>> ============ ========================

> value.

Here's the resolution. 6.7.2.1 p8 talks about struct/union
members, including bit-fields. The last sentence of that paragraph
states that the _width_ of the bit-field is what comes after the
colon. The term 'width' is defined in 6.2.6.2 p6. For signed
types, the width is one greater than the number of value bits in
the type (the 'precision'). Hence, a bit-field of width 1 having a
signed type will have 1 sign bit and 0 value bits.

Tim Rentsch, Jan 25, 2012
4. ### Tim RentschGuest

James Kuyper <> writes:

> On 11/29/2011 02:55 PM, Lew Pitcher wrote:
> ...
>> In 6.2.6.2 (Integer types), the standard says that a signed integer type
>> will have a single sign bit, some optional padding, and value bits. It is
>> emphatic that the padding is optional, and emphatic that the sign bit is
>> required, but I cannot tell if the value bits are /required by the
>> standard/ or not.

>
> If it had only specified that padding was optional, the rest of the
> sentence would clearly have implied that value bits are mandatory. If it
> had only specified that sign bits are mandatory, that would clearly have
> implied that value bits are optional. Specifying both leaves it unclear
> whether having at least one value bit is mandatory or optional.

Not at all; there is one sign bit and zero value bits.
See 6.7.2.1 p8 and 6.2.6.2 p6.

Tim Rentsch, Jan 25, 2012