Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

signed single-bit field

39 views
Skip to first unread message

kyle york

unread,
Nov 29, 2011, 2:10:22 PM11/29/11
to
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.

--kyle


Message has been deleted

James Kuyper

unread,
Nov 29, 2011, 2:33:49 PM11/29/11
to
6.2.6.2p2 specifies that the sign bit has a value of -(2^N -1) for one's
complement, where N is the number of value bits. For a one-bit signed
type, N=0, so the sign bit has a value of -(2^0 - 1) == 0. Therefore, a
1-bit one's complement type always represents 0, whether or not that bit
is set. This makes it less than useful.

Sign-magnitude fares no better: the sign bit merely determines whether
it represents a positive zero or a negative zero. Since positive zero is
required to compare equal to negative zero, that doesn't help much either.

Only twos-complement one-bit fields are useful: The sign bit has a value
of -(2^N). For N=0, that's -1, so it can represent either a value of 0
or of -1.

Lew Pitcher

unread,
Nov 29, 2011, 2:55:14 PM11/29/11
to
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
carry sign information only.

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.


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. ------


James Kuyper

unread,
Nov 29, 2011, 3:20:47 PM11/29/11
to
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
> carry sign information only.

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.

kyle york

unread,
Nov 30, 2011, 10:47:44 AM11/30/11
to
Greetings,

Thanks all. Good references!

Tim Rentsch

unread,
Jan 25, 2012, 3:16:43 PM1/25/12
to
> 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

unread,
Jan 25, 2012, 3:22:43 PM1/25/12
to
James Kuyper <james...@verizon.net> 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.
0 new messages