Re: A portable code to create a 4-bytes Big Endian twos complementsigned integer in a misaligned add

Discussion in 'C Programming' started by James Kuyper, Mar 17, 2011.

  1. James Kuyper

    James Kuyper Guest

    On 03/17/2011 05:01 AM, pozz wrote:
    > I need a variable that can represents numbers between -100000 and
    > +100000. int variables could be only 16-bit on some implementations, so
    > I use int32_t type:
    > int32_t x;
    > Is it correct?


    If the requirements given above are the only relevant ones, than int32_t
    is not the best choice, because an implementation is not required to
    support it. A better choice would be int_fast32_t; all implementations
    are required to support it.

    To be fair, it's extremely unlikely that int32_t would not be supported.
    There have been machines with 36-bit words, though I have not heard of a
    conforming implementation of any version of C on those machines. The
    type int32_t could not be implemented on such a machine. If C99 were
    implemented on such a machine, a plausible implementation might have
    CHAR_BITS == 9, and int_fast32_t would probably be a 36-bit type.
    --
    James Kuyper
     
    James Kuyper, Mar 17, 2011
    #1
    1. Advertising

  2. On 17/03/2011 13:34, James Kuyper wrote:
    > On 03/17/2011 05:01 AM, pozz wrote:
    >> I need a variable that can represents numbers between -100000 and
    >> +100000. int variables could be only 16-bit on some implementations, so
    >> I use int32_t type:
    >> int32_t x;
    >> Is it correct?

    >
    > If the requirements given above are the only relevant ones, than int32_t is not the best choice, because an implementation is not required to support it. A
    > better choice would be int_fast32_t; all implementations are required to support it.


    Thanks for pointing out that an implementation is not required to support
    int32_t; I missed that.

    > To be fair, it's extremely unlikely that int32_t would not be supported. There have been machines with 36-bit words, though I have not heard of a conforming
    > implementation of any version of C on those machines. The type int32_t could not be implemented on such a machine. If C99 were implemented on such a machine, a
    > plausible implementation might have CHAR_BITS == 9, and int_fast32_t would probably be a 36-bit type.


    Given that int32_t, if it is supported, is required to use two's complement,
    (per 7.18.1.1p1), is it guaranteed that (x>>8)&255 is bits 8 to 15 of the
    two's complement representation of x, including for negative x ?
    Or is necessary to write ((uint32_fact_t)x>>8)&255 ?

    TIA,
    Francois Grieu
     
    Francois Grieu, Mar 17, 2011
    #2
    1. Advertising

  3. On 17/03/2011 13:34, James Kuyper wrote:
    > On 03/17/2011 05:01 AM, pozz wrote:
    >> I need a variable that can represents numbers between -100000 and
    >> +100000. int variables could be only 16-bit on some implementations, so
    >> I use int32_t type:
    >> int32_t x;
    >> Is it correct?

    >
    > If the requirements given above are the only relevant ones, than int32_t is not the best choice, because an implementation is not required to support it. A
    > better choice would be int_fast32_t; all implementations are required to support it.


    Thanks for pointing out that an implementation is not required to support
    int32_t; I missed that.

    > To be fair, it's extremely unlikely that int32_t would not be supported. There have been machines with 36-bit words, though I have not heard of a conforming
    > implementation of any version of C on those machines. The type int32_t could not be implemented on such a machine. If C99 were implemented on such a machine, a
    > plausible implementation might have CHAR_BITS == 9, and int_fast32_t would probably be a 36-bit type.


    Let us assume int32_t is supported. It is required to use two's complement,
    per 7.18.1.1p1. Is it guaranteed that (x>>8)&255 is bits 8 to 15 of the
    two's complement representation of x, including for negative x ?
    Or is necessary to write ((uint32_fast_t)x>>8)&255 ?

    TIA,
    Francois Grieu
    [reposted with correction]
     
    Francois Grieu, Mar 17, 2011
    #3
  4. James Kuyper

    James Kuyper Guest

    On 03/17/2011 09:31 AM, Francois Grieu wrote:
    > On 17/03/2011 13:34, James Kuyper wrote:
    >> On 03/17/2011 05:01 AM, pozz wrote:
    >>> I need a variable that can represents numbers between -100000 and
    >>> +100000. int variables could be only 16-bit on some implementations, so
    >>> I use int32_t type:
    >>> int32_t x;
    >>> Is it correct?

    >>
    >> If the requirements given above are the only relevant ones, than int32_t is not the best choice, because an implementation is not required to support it. A
    >> better choice would be int_fast32_t; all implementations are required to support it.

    >
    > Thanks for pointing out that an implementation is not required to support
    > int32_t; I missed that.
    >
    >> To be fair, it's extremely unlikely that int32_t would not be supported. There have been machines with 36-bit words, though I have not heard of a conforming
    >> implementation of any version of C on those machines. The type int32_t could not be implemented on such a machine. If C99 were implemented on such a machine, a
    >> plausible implementation might have CHAR_BITS == 9, and int_fast32_t would probably be a 36-bit type.

    >
    > Given that int32_t, if it is supported, is required to use two's complement,
    > (per 7.18.1.1p1), is it guaranteed that (x>>8)&255 is bits 8 to 15 of the
    > two's complement representation of x, including for negative x ?


    Right shift of a negative number produces an implementation-defined
    result. That result is likely to be what you expect, but the standard
    does not define it, not even when you specify use of two's complement
    notation.

    Keep in mind that different people number bits in different ways; some
    number them in order of increasing value, others in order of decreasing
    value. Some, especially C programmers, start the numbering at 0, others
    start at 1. In general, when asking a question like the one above, you
    should specify which numbering convention you're using; though in this
    context only one of the possibilities makes sense.

    > Or is necessary to write ((uint32_fact_t)x>>8)&255 ?


    I think that would work. I don't do that kind of coding very often,
    though, so I may have missed something.

    --
    James Kuyper
     
    James Kuyper, Mar 19, 2011
    #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. hicham
    Replies:
    2
    Views:
    9,028
    dxcoder
    Jul 2, 2003
  2. Replies:
    5
    Views:
    357
    Stephen Sprunk
    Aug 31, 2006
  3. Jorgen Grahn
    Replies:
    2
    Views:
    374
  4. Spiros Bousbouras
    Replies:
    6
    Views:
    425
    Ben Bacarisse
    Mar 18, 2011
  5. Tim Rentsch
    Replies:
    0
    Views:
    470
    Tim Rentsch
    Mar 19, 2011
Loading...

Share This Page