# initializing an int variable with all F's

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

1. ### Pietro CeruttiGuest

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 ?

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

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

3. ### Ark KhasinGuest

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
4. ### Malcolm McLeanGuest

<> 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
5. ### CBFalconerGuest

"" 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
6. ### raviGuest

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
7. ### Pietro CeruttiGuest

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
8. ### Pietro CeruttiGuest

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
9. ### peteGuest

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
10. ### peteGuest

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
11. ### peteGuest

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
12. ### Philip PotterGuest

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

Phil

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

Philip Potter, Aug 29, 2007
13. ### pemoGuest

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
14. ### Philip PotterGuest

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
15. ### Kenneth BrodyGuest

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
16. ### Harald van =?UTF-8?B?RMSzaw==?=Guest

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
17. ### Keith ThompsonGuest

"" <> 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) <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
18. ### Old WolfGuest

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
19. ### peteGuest

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
20. ### peteGuest

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?