initializing an int variable with all F's

Discussion in 'C Programming' started by Pietro Cerutti, Aug 29, 2007.

  1. Keith Thompson wrote:
    > "" <> writes:
    >> I was just looking at some code where to initialize an integer
    >> with all F's following statement is used;
    >>
    >> unsigned int i = -1;
    >>
    >> I want to know if this is the right way of doing the things ? if not,
    >> what is the correct way to do this ?

    >
    > Can you be more specific about your requirement?
    >
    > You wrote "int" in your subject header, "integer" in the body of your
    > article, and "unsigned int" in your example.
    >
    > "all F's" is a very strange requirement, or at least a very strange
    > way to express it. I assume you mean "0xF...F", where the number of
    > Fs depends on the width of the type. What if the width of the type
    > (in bits) isn't a multiple of 4?


    Could you name an integer type whose width (in bits) sn't a multiple of 4?

    >
    > Do you want the maximum value of some type? If so, which one?
    >



    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
    Pietro Cerutti, Aug 29, 2007
    #1
    1. Advertising

  2. Pietro Cerutti

    Guest

    Guys,

    I was just looking at some code where to initialize an integer
    with all F's following statement is used;

    unsigned int i = -1;

    I want to know if this is the right way of doing the things ? if not,
    what is the correct way to do this ?
    , Aug 29, 2007
    #2
    1. Advertising

  3. Pietro Cerutti

    Ark Khasin Guest

    wrote:
    > Guys,
    >
    > I was just looking at some code where to initialize an integer
    > with all F's following statement is used;
    >
    > unsigned int i = -1;
    >
    > I want to know if this is the right way of doing the things ? if not,
    > what is the correct way to do this ?
    >

    unsigned int i = ~0U;
    initializes to all 1's. There is no way to initialize to all
    (presumably, hex) F's on a hypothetical 41-bit machine.
    Ark Khasin, Aug 29, 2007
    #3
  4. <> wrote in message
    news:...
    > Guys,
    >
    > I was just looking at some code where to initialize an integer
    > with all F's following statement is used;
    >
    > unsigned int i = -1;
    >
    > I want to know if this is the right way of doing the things ? if not,
    > what is the correct way to do this ?
    >

    It is one of those awkward things.
    In practise you are never likely to program a non-two's complement machine.
    However one's complement and sign magnitude are allowed (one place you will
    see non-two's complement integers is in the exponent of an IEEE floating
    point number).
    So i = ~0; is actually the best way of achieving things. But you will see i
    = -1 even in production code.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Aug 29, 2007
    #4
  5. Pietro Cerutti

    CBFalconer Guest

    "" wrote:
    >
    > I was just looking at some code where to initialize an integer
    > with all F's following statement is used;
    >
    > unsigned int i = -1;
    >
    > I want to know if this is the right way of doing the things ?
    > if not, what is the correct way to do this ?


    It's not an integer, it's an unsigned int. Not the same. And yes,
    the unsigned int will be initialized to 2**n - 1. If "sizeof
    unsigned int * CHAR_BIT" is a multiple of 8, that will be a
    collection of hex f.

    Copying that unsigned int into an int will normally cause undefined
    behaviour, because it out of range for an int. That behaviour may
    be exactly what you want, or anything else. You just don't know.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Aug 29, 2007
    #5
  6. Pietro Cerutti

    ravi Guest

    On Aug 29, 11:10 am, ""
    <> wrote:
    > Guys,
    >
    > I was just looking at some code where to initialize an integer
    > with all F's following statement is used;
    >
    > unsigned int i = -1;
    >
    > I want to know if this is the right way of doing the things ? if not,
    > what is the correct way to do this ?


    Another way is :-

    int i = 0xFFFF;
    ravi, Aug 29, 2007
    #6
  7. ravi wrote:
    > On Aug 29, 11:10 am, ""
    > <> wrote:
    >> Guys,
    >>
    >> I was just looking at some code where to initialize an integer
    >> with all F's following statement is used;
    >>
    >> unsigned int i = -1;
    >>
    >> I want to know if this is the right way of doing the things ? if not,
    >> what is the correct way to do this ?

    >
    > Another way is :-
    >
    > int i = 0xFFFF;


    Assuming 4-bytes int.


    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
    Pietro Cerutti, Aug 29, 2007
    #7
  8. Pietro Cerutti wrote:
    > ravi wrote:
    >> On Aug 29, 11:10 am, ""
    >> <> wrote:
    >>> Guys,
    >>>
    >>> I was just looking at some code where to initialize an integer
    >>> with all F's following statement is used;
    >>>
    >>> unsigned int i = -1;
    >>>
    >>> I want to know if this is the right way of doing the things ? if not,
    >>> what is the correct way to do this ?

    >> Another way is :-
    >>
    >> int i = 0xFFFF;

    >
    > Assuming 4-bytes int.


    hem... assuming 2-bytes ints.

    Check out this:

    #include <stdio.h>
    int main(void) {
    unsigned int i = 0xFFFFU;
    unsigned int ii = ~0U;
    printf("i is %u, ll is %u\n", i, ii);
    return (0);
    }




    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
    Pietro Cerutti, Aug 29, 2007
    #8
  9. Pietro Cerutti

    pete Guest

    Malcolm McLean wrote:
    >
    > <> wrote in message
    > news:...
    > > Guys,
    > >
    > > I was just looking at some code where to initialize an integer
    > > with all F's following statement is used;
    > >
    > > unsigned int i = -1;


    > So i = ~0; is actually the best way of achieving things.


    That's wrong.

    unsigned int i = -1;
    is guaranteed to put a value of UINT_MAX into object i.

    i = ~0; operates on the sign bit of an int type value
    and yields an implementation defined result.

    N869
    6.2.5 Types
    A computation involving unsigned operands
    can never overflow, because a result that cannot be
    represented by the resulting unsigned integer type is
    reduced modulo the number that is one greater than the
    largest value that can be represented by the resulting type.

    --
    pete
    pete, Aug 29, 2007
    #9
  10. Pietro Cerutti

    pete Guest

    Ark Khasin wrote:
    >
    > wrote:
    > > Guys,
    > >
    > > I was just looking at some code where to initialize an integer
    > > with all F's following statement is used;
    > >
    > > unsigned int i = -1;
    > >
    > > I want to know if this is the right way of doing the things ?
    > > if not, what is the correct way to do this ?
    > >

    > unsigned int i = ~0U;
    > initializes to all 1's.


    So does unsigned int i = -1;

    --
    pete
    pete, Aug 29, 2007
    #10
  11. Pietro Cerutti

    pete Guest

    wrote:
    >
    > Guys,
    >
    > I was just looking at some code where to initialize an integer
    > with all F's following statement is used;
    >
    > unsigned int i = -1;
    >
    > I want to know if this is the right way of doing the things ?


    That is one correct way to initialise an unsigned int
    object with a value of UINT_MAX

    --
    pete
    pete, Aug 29, 2007
    #11
  12. Malcolm McLean wrote:
    >
    > <> wrote in message
    > news:...
    >>
    >> I was just looking at some code where to initialize an integer
    >> with all F's following statement is used;
    >>
    >> unsigned int i = -1;
    >>
    >> I want to know if this is the right way of doing the things ? if not,
    >> what is the correct way to do this ?
    >>

    > It is one of those awkward things.
    > In practise you are never likely to program a non-two's complement
    > machine. However one's complement and sign magnitude are allowed (one
    > place you will see non-two's complement integers is in the exponent of
    > an IEEE floating point number).


    One's complement and sign-magnitude are only allowed for /signed/ integers. The
    OP clearly was asking about unsigned.

    Phil

    --
    Philip Potter pgp <at> doc.ic.ac.uk
    Philip Potter, Aug 29, 2007
    #12
  13. Pietro Cerutti

    pemo Guest

    On Aug 29, 11:54 am, pete <> wrote:
    > Malcolm McLean wrote:
    >
    > > <> wrote in message
    > >news:...
    > > > Guys,

    >
    > > > I was just looking at some code where to initialize an integer
    > > > with all F's following statement is used;

    >
    > > > unsigned int i = -1;

    > > So i = ~0; is actually the best way of achieving things.

    >
    > That's wrong.
    >
    > unsigned int i = -1;
    > is guaranteed to put a value of UINT_MAX into object i.


    <snip>

    So, include limits.h and use

    unsigned int i = UINT_MAX;

    Can't see any real difference - except that the latter is *perhaps*
    more self-documenting?

    pemo
    pemo, Aug 29, 2007
    #13
  14. wrote:
    > Guys,
    >
    > I was just looking at some code where to initialize an integer
    > with all F's following statement is used;
    >
    > unsigned int i = -1;
    >
    > I want to know if this is the right way of doing the things ? if not,
    > what is the correct way to do this ?


    Why do you want to set a variable to all F's? I can't think of many reasons I'd
    want to store UINT_MAX in a variable. (Curiosity is the only reason I can think
    of offhand.)

    Phil

    --
    Philip Potter pgp <at> doc.ic.ac.uk
    Philip Potter, Aug 29, 2007
    #14
  15. pete wrote:
    >
    > Ark Khasin wrote:
    > >
    > > wrote:
    > > > Guys,
    > > >
    > > > I was just looking at some code where to initialize an integer
    > > > with all F's following statement is used;
    > > >
    > > > unsigned int i = -1;
    > > >
    > > > I want to know if this is the right way of doing the things ?
    > > > if not, what is the correct way to do this ?
    > > >

    > > unsigned int i = ~0U;
    > > initializes to all 1's.

    >
    > So does unsigned int i = -1;


    What if you're not on a two's complement system? Perhaps "-1" is
    not represented by all-bits-one?

    (OTOH, is "0U" guaranteed to be represented as all-bits-zero, and
    therefore "~0U" all-bits-one?)

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Aug 29, 2007
    #15
  16. CBFalconer wrote:
    > "" wrote:
    >>
    >> I was just looking at some code where to initialize an integer
    >> with all F's following statement is used;
    >>
    >> unsigned int i = -1;
    >>
    >> I want to know if this is the right way of doing the things ?
    >> if not, what is the correct way to do this ?

    >
    > It's not an integer, it's an unsigned int. Not the same.


    All unsigned ints are integers. What unsigned ints are not is ints. Integer
    does not mean int.

    > And yes,
    > the unsigned int will be initialized to 2**n - 1. If "sizeof
    > unsigned int * CHAR_BIT" is a multiple of 8, that will be a
    > collection of hex f.
    >
    > Copying that unsigned int into an int will normally cause undefined
    > behaviour, because it out of range for an int. That behaviour may
    > be exactly what you want, or anything else. You just don't know.


    That is not what the standard says. You have been informed of this just a
    few days ago by Keith Thompson in message <>.
    Do you disagree with it, or did you not read that message (yet)?
    Harald van =?UTF-8?B?RMSzaw==?=, Aug 29, 2007
    #16
  17. "" <> writes:
    > I was just looking at some code where to initialize an integer
    > with all F's following statement is used;
    >
    > unsigned int i = -1;
    >
    > I want to know if this is the right way of doing the things ? if not,
    > what is the correct way to do this ?


    Can you be more specific about your requirement?

    You wrote "int" in your subject header, "integer" in the body of your
    article, and "unsigned int" in your example.

    "all F's" is a very strange requirement, or at least a very strange
    way to express it. I assume you mean "0xF...F", where the number of
    Fs depends on the width of the type. What if the width of the type
    (in bits) isn't a multiple of 4?

    Do you want the maximum value of some type? If so, which one?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 29, 2007
    #17
  18. Pietro Cerutti

    Old Wolf Guest

    On Aug 30, 3:47 am, Kenneth Brody <> wrote:
    > pete wrote:
    > > Ark Khasin wrote:

    >
    > > > unsigned int i = ~0U;
    > > > initializes to all 1's.

    >
    > > So does unsigned int i = -1;

    >
    > What if you're not on a two's complement system? Perhaps "-1" is
    > not represented by all-bits-one?


    It doesn't matter what -1 is represented as. Initializing
    a uint with the value -1 is defined as initializing with
    the value UINT_MAX.
    Old Wolf, Aug 29, 2007
    #18
  19. Pietro Cerutti

    pete Guest

    pemo wrote:
    >
    > On Aug 29, 11:54 am, pete <> wrote:
    > > Malcolm McLean wrote:
    > >
    > > > <> wrote in message
    > > >news:...
    > > > > Guys,

    > >
    > > > > I was just looking at some code
    > > > > where to initialize an integer
    > > > > with all F's following statement is used;

    > >
    > > > > unsigned int i = -1;
    > > > So i = ~0; is actually the best way of achieving things.

    > >
    > > That's wrong.
    > >
    > > unsigned int i = -1;
    > > is guaranteed to put a value of UINT_MAX into object i.

    >
    > <snip>
    >
    > So, include limits.h and use
    >
    > unsigned int i = UINT_MAX;
    >
    > Can't see any real difference - except that the latter is *perhaps*
    > more self-documenting?


    The meaning of your way is more obvious.
    I'm all for that.

    --
    pete
    pete, Aug 29, 2007
    #19
  20. Pietro Cerutti

    pete Guest

    Kenneth Brody wrote:
    >
    > pete wrote:
    > >
    > > Ark Khasin wrote:
    > > >
    > > > wrote:
    > > > > Guys,
    > > > >
    > > > > I was just looking at some code where to
    > > > > initialize an integer
    > > > > with all F's following statement is used;
    > > > >
    > > > > unsigned int i = -1;
    > > > >
    > > > > I want to know if this is the right way of doing the things ?
    > > > > if not, what is the correct way to do this ?
    > > > >
    > > > unsigned int i = ~0U;
    > > > initializes to all 1's.

    > >
    > > So does unsigned int i = -1;

    >
    > What if you're not on a two's complement system? Perhaps "-1" is
    > not represented by all-bits-one?


    I'm glad that you asked.
    Two's complement has nothing to do with it.

    --
    pete
    pete, Aug 29, 2007
    #20
    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. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,625
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    450
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    942
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    428
    James Kanze
    Apr 17, 2007
Loading...

Share This Page