Bit Padding and other questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vippstar@gmail.com

    Bit Padding and other questions

    Hey comp.lang.c

    I'm somewhat confused with bit padding.
    I tried searching the FAQ, but there isn't a search feature, so I used
    google and the search query: site:c-faq.com padding.
    I did not find anything relevant to bit padding, only byte padding for
    structs, which more or less I understand.

    All sections are from n1256

    6.2.6.1 p4 says all objects except bit-fields consist of n * CHAR_BIT
    bits, where n is sizeof object.
    So if an int has padding bits, they are multiples of CHAR_BIT? Is
    there a way to find which bits are the padding bits?
    Padding bits can only be at the start/end of the object
    representation, or anywhere? What happends if I access those padding
    bits, by casting the object to (unsigned char *)? What happends if I
    change the value of these padding bits?

    6.2.6.1 p5 says the object representation does not have to represent
    the value of the object type. I think I understand this, for example
    with offset:segment pointers, where they could be mapped to the same
    area, but have different values? If I understand correctly, == takes
    care of this.
    In sort, == compares values and memcmp() compares object
    representations , right?
    However, 6.2.6.2 note 43 says that x == y does not necessarily imply
    that x and y have the same value.
    I don't understand this, in my opinion contradicts the previous
    paragraphs, does it not?

    Why would an implementation want padding bits in any object type? Why
    cannot these bits be used to increase the value range?
    Why does the standard explicity mention that the byte that getchar() &
    family functions read is returned casted to (unsigned char)? I think
    it has something to do with 'unsigned char' being the only type that
    doesn't have padding bits, but I'm not sure how/why that matters.

    Lastly, can the bitshift operators manipulate the padding bits?
    For example: int i = x; i <<= y;

    All replies much appreciated.
  • santosh

    #2
    Re: Bit Padding and other questions

    vippstar@gmail. com wrote:
    Hey comp.lang.c
    >
    I'm somewhat confused with bit padding.
    I tried searching the FAQ, but there isn't a search feature,
    What about:

    <http://c-faq.com/search.html>
    so I used
    google and the search query: site:c-faq.com padding.
    I did not find anything relevant to bit padding,
    That's because the CLC FAQ was written much before C99 which first
    introduced padding bits.

    <I'll leave it to the "real experts" to answer your further questions>
    :-)

    Comment

    • Ben Bacarisse

      #3
      Re: Bit Padding and other questions

      vippstar@gmail. com writes:
      I'm somewhat confused with bit padding.
      <snip>

      You've had an excellent answer to all of the technical points so I'll
      just add a bit to one small question:
      Why would an implementation want padding bits in any object type? Why
      cannot these bits be used to increase the value range?
      When computers were not stamped on wafers, the two main reasons to have
      padding bits were cost and speed.

      Logic was both expensive and slow, so having more than you need was a
      waste. The classic example was the 32-bit IBM 360 and 370 series.
      32-bit ints are useful but since no one could afford (or even build)
      32 bits worth of memory (4Gb) there was no point in having address
      logic to operate on more than, say, 24-bit addresses. At the same
      time, the fastest way to get addresses out of memory was to request
      a full 32-bit word so, as a result, all pointers had 8 padding bits.
      These are addresses, but pointers are still object types in C.

      Similar arguments led some supercomputers[1] to have short integer and
      floating-point operations that used faster logic than the full-width
      versions. Actually that much was, in fact, common. The key is that on
      some systems, due to the design of the memory, getting the fastest
      access times sometimes required that these values be stored in objects
      that were larger than they needed to be -- i.e. they had padding.

      [1] Anyone have a reference? This is "what people always say" but I
      have never used such a system, nor seen any documentation for one.

      --
      Ben.

      Comment

      • vippstar@gmail.com

        #4
        Re: Bit Padding and other questions

        On Jun 21, 7:45 pm, vipps...@gmail. com wrote:
        Hey comp.lang.c
        >
        I'm somewhat confused with bit padding.
        I tried searching the FAQ, but there isn't a search feature, so I used
        google and the search query: site:c-faq.com padding.
        I did not find anything relevant to bit padding, only byte padding for
        structs, which more or less I understand.
        >
        <snip questions>
        >
        All replies much appreciated.
        Thanks, very enlightening replies.

        Comment

        Working...