Velocity Reviews > initializing an int variable with all F's

# initializing an int variable with all F's

pete
Guest
Posts: n/a

 08-29-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

Philip Potter
Guest
Posts: n/a

 08-29-2007
Malcolm McLean wrote:
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>>
>> 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

Phil

--
Philip Potter pgp <at> doc.ic.ac.uk

pemo
Guest
Posts: n/a

 08-29-2007
On Aug 29, 11:54 am, pete <(E-Mail Removed)> wrote:
> Malcolm McLean wrote:
>
> > <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed) oups.com...
> > > 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

Philip Potter
Guest
Posts: n/a

 08-29-2007
(E-Mail Removed) 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

Kenneth Brody
Guest
Posts: n/a

 08-29-2007
pete wrote:
>
> Ark Khasin wrote:
> >
> > (E-Mail Removed) 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: <(E-Mail Removed)>

Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a

 08-29-2007
CBFalconer wrote:
> "(E-Mail Removed)" 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 <(E-Mail Removed)>.
Do you disagree with it, or did you not read that message (yet)?

Keith Thompson
Guest
Posts: n/a

 08-29-2007
"(E-Mail Removed)" <(E-Mail Removed)> 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 ?

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) (E-Mail Removed) <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"

Old Wolf
Guest
Posts: n/a

 08-29-2007
On Aug 30, 3:47 am, Kenneth Brody <(E-Mail Removed)> 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.

pete
Guest
Posts: n/a

 08-29-2007
pemo wrote:
>
> On Aug 29, 11:54 am, pete <(E-Mail Removed)> wrote:
> > Malcolm McLean wrote:
> >
> > > <(E-Mail Removed)> wrote in message
> > >news:(E-Mail Removed) oups.com...
> > > > 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
Guest
Posts: n/a

 08-29-2007
Kenneth Brody wrote:
>
> pete wrote:
> >
> > Ark Khasin wrote:
> > >
> > > (E-Mail Removed) 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?

Two's complement has nothing to do with it.

--
pete