Re: signed single-bit field

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

  1. Lew Pitcher

    Lew Pitcher Guest

    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
    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-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
    #1
    1. Advertising

  2. Lew Pitcher

    James Kuyper Guest

    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.
    James Kuyper, Nov 29, 2011
    #2
    1. Advertising

  3. Lew Pitcher

    Tim Rentsch Guest

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


    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
    #3
  4. Lew Pitcher

    Tim Rentsch Guest

    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
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,750
    Timothy Bendfelt
    Jan 19, 2007
  2. Replies:
    9
    Views:
    968
    Juha Nieminen
    Aug 22, 2007
  3. kyrpa83
    Replies:
    1
    Views:
    623
    kyrpa83
    Oct 17, 2007
  4. Fore
    Replies:
    29
    Views:
    15,252
    Rashad
    Sep 21, 2008
  5. Rob1bureau
    Replies:
    1
    Views:
    807
    joris
    Feb 27, 2010
Loading...

Share This Page