By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 974 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

initializing an int variable with all F's

P: n/a
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 '07 #1
Share this Question
Share on Google+
38 Replies


P: n/a
ju**********@yahoo.co.in 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.
Aug 29 '07 #2

P: n/a

<ju**********@yahoo.co.inwrote in message
news:11**********************@q4g2000prc.googlegro ups.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;

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
Aug 29 '07 #3

P: n/a
"ju**********@yahoo.co.in" 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

Aug 29 '07 #4

P: n/a
On Aug 29, 11:10 am, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
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;

Aug 29 '07 #5

P: n/a
ravi wrote:
On Aug 29, 11:10 am, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
>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
Aug 29 '07 #6

P: n/a
Pietro Cerutti wrote:
ravi wrote:
>On Aug 29, 11:10 am, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
>>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
Aug 29 '07 #7

P: n/a
Malcolm McLean wrote:
>
<ju**********@yahoo.co.inwrote in message
news:11**********************@q4g2000prc.googlegro ups.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.

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
Aug 29 '07 #8

P: n/a
Ark Khasin wrote:
>
ju**********@yahoo.co.in 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
Aug 29 '07 #9

P: n/a
ju**********@yahoo.co.in 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
Aug 29 '07 #10

P: n/a
Malcolm McLean wrote:
>
<ju**********@yahoo.co.inwrote in message
news:11**********************@q4g2000prc.googlegro 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
OP clearly was asking about unsigned.

Phil

--
Philip Potter pgp <atdoc.ic.ac.uk
Aug 29 '07 #11

P: n/a
ju**********@yahoo.co.in 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 <atdoc.ic.ac.uk
Aug 29 '07 #12

P: n/a
pete wrote:
>
Ark Khasin wrote:

ju**********@yahoo.co.in 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:Th*************@gmail.com>

Aug 29 '07 #13

P: n/a
CBFalconer wrote:
"ju**********@yahoo.co.in" 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 <ln************@nuthaus.mib.org>.
Do you disagree with it, or did you not read that message (yet)?
Aug 29 '07 #14

P: n/a
"ju**********@yahoo.co.in" <ju**********@yahoo.co.inwrites:
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) ks***@mib.org <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"
Aug 29 '07 #15

P: n/a
On Aug 30, 3:47 am, Kenneth Brody <kenbr...@spamcop.netwrote:
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.

Aug 29 '07 #16

P: n/a
Kenneth Brody wrote:
>
pete wrote:

Ark Khasin wrote:
>
ju**********@yahoo.co.in 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
Aug 29 '07 #17

P: n/a
Keith Thompson wrote:
"ju**********@yahoo.co.in" <ju**********@yahoo.co.inwrites:
> 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
Aug 29 '07 #18

P: n/a
Malcolm McLean wrote:
>
<ju**********@yahoo.co.inwrote in message
news:11**********************@q4g2000prc.googlegro ups.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;

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.
Two's complement has nothing to do with it.

--
pete
Aug 29 '07 #19

P: n/a
In article <77***************************@news.hispeed.ch>,
Pietro Cerutti <ga**@gahr.chwrote:
>Could you name an integer type whose width (in bits) sn't a multiple of 4?
Yes. DEC made several machines (such as the TOPS series) with 18 bit
int. The Honeywell L6 series used a 36 bit word; I do not recall
at the moment if it was an 18 or 36 bit int on the compilers.
--
All is vanity. -- Ecclesiastes
Aug 29 '07 #20

P: n/a
Pietro Cerutti <ga**@gahr.chwrites:
Keith Thompson wrote:
[...]
>"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?
_Bool.

The standard says that a bit-field such as:
struct {
unsigned int bf:3;
};
has type unsigned int, so I suppose that doesn't count.

Other than that, I don't know of any implementation that has an
integer type whose width isn't a multiple of 4, but such types would
be perfectl legal (e.g., CHAR_BIT==9).

If the OP really wants to know how to set an integer to "all F's", he
should either explain what that means for types whose widths isn't a
multiple of 4, or tell us that he's not interested in such types.

More likely, what he wants to know isn't exactly what he asked.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Aug 29 '07 #21

P: n/a
Keith Thompson wrote:
Could you name an integer type whose width
(in bits) sn't a multiple of 4?

_Bool.

The standard says that a bit-field such as:
struct {
unsigned int bf:3;
};
has type unsigned int, so I suppose that doesn't count.

Other than that, I don't know of any implementation that has an
integer type whose width isn't a multiple of 4, but such types would
be perfectl legal (e.g., CHAR_BIT==9).
CHAR_BIT == 8
sizeof(int) == 2

That's not exotic.
It's just old fashioned.

--
pete
Aug 30 '07 #22

P: n/a
pete wrote:
>
Keith Thompson wrote:
Other than that, I don't know of any implementation that has an
integer type whose width isn't a multiple of 4,
sizeof(int) == 2
I forgot what "width" meant for a minute.

--
pete
Aug 30 '07 #23

P: n/a
Harald van D?k wrote:
CBFalconer wrote:
.... snip ...
>
>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 <ln************@nuthaus.mib.org>.
Do you disagree with it, or did you not read that message (yet)?
I'm sloppy. I don't really differentiate between undefined and
implementation defined, etc. I just try to avoid them.

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

Aug 30 '07 #24

P: n/a
Kenneth Brody wrote:
pete wrote:
>Ark Khasin wrote:
.... snip ...
>>>
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?)
I believe that was recently resolved by the Standards Committee.

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

Aug 30 '07 #25

P: n/a
ravi wrote:
<junky_fel...@yahoo.co.inwrote:
>>
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;
That was 'unsigned int'. And what if the int occupies more than 2
octets? We try to write portable code here.

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

Aug 30 '07 #26

P: n/a
pete wrote:
Ark Khasin wrote:
>ju**********@yahoo.co.in 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 ?

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

So does unsigned int i = -1;
Not on 1's complement or sign-magnitude machinery.

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

Aug 30 '07 #27

P: n/a
CBFalconer <cb********@yahoo.comwrites:
pete wrote:
>Ark Khasin wrote:
>>ju**********@yahoo.co.in 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 ?

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

So does unsigned int i = -1;

Not on 1's complement or sign-magnitude machinery.
Incorrect. '-1' is an expression of type int, with the obvious value.
It's implicitly converted to unsigned int, yielding UINT_MAX. See C99
6.3.1.3. The representation of UINT_MAX will be all 1's unless there
are padding bits, regardless of the representation used for signed
types.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Aug 30 '07 #28

P: n/a
On Wed, 29 Aug 2007 12:25:57 +0200, Pietro Cerutti <ga**@gahr.ch>
wrote in comp.lang.c:
Pietro Cerutti wrote:
ravi wrote:
On Aug 29, 11:10 am, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
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);
}
Both of your assumptions are wrong. It is assuming 16-bit ints, which
happen to be exactly one byte on a platform that I do a lot of work
on.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Aug 30 '07 #29

P: n/a
On Wed, 29 Aug 2007 13:37:55 +0100, Philip Potter
<pg*@see.sig.invalidwrote in comp.lang.c:
ju**********@yahoo.co.in 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.)
Really? What about seeding a maximum value for an routine that finds
the smallest in an array of unsigned ints? Initializing with this
value prevents having to handle the first value in the array
differently from the others.

unsigned int find_smallest_ui(unsigned *ptr, size_t count)
{
unsigned int result = -1;
while (count--)
{
if (result *ptr)
{
result = *ptr;
}
++ptr;
}
return result;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Aug 30 '07 #30

P: n/a
On Aug 30, 3:24 am, Keith Thompson <ks...@mib.orgwrote:
"junky_fel...@yahoo.co.in" <junky_fel...@yahoo.co.inwrites:
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?
This variable is used to store the state of some object. The value all
F's is used as an Invalid state. So, motive is to initialize it with
the invalid state.

On my machine, unsigned integer is 32 bits. So, my requirement is to
initialize it with all 32 F's.

But now my question is if
unsigned int i = -1;
would fail if the size of integer is not a multiple of 4 ?

Aug 30 '07 #31

P: n/a
Pietro Cerutti wrote:
>
.... snip ...
>
Could you name an integer type whose width (in bits) sn't a
multiple of 4?
Yes. These include char, short, int, long, long long. All on
suitable hardware.

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

Aug 30 '07 #32

P: n/a
"ju**********@yahoo.co.in" <ju**********@yahoo.co.inwrites:
On Aug 30, 3:24 am, Keith Thompson <ks...@mib.orgwrote:
>"junky_fel...@yahoo.co.in" <junky_fel...@yahoo.co.inwrites:
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?

This variable is used to store the state of some object. The value all
F's is used as an Invalid state. So, motive is to initialize it with
the invalid state.
*Why* is "all F's" used to indicate an invalid state? Where did this
requirement come from? Is the requirement actually to use "all F's",
or is that your interpretation?
On my machine, unsigned integer is 32 bits. So, my requirement is to
initialize it with all 32 F's.
Please be precise; it's important. There are several "unsigned
integer" types. One of them is "unsigned int". (Some other unsigned
integer types are unsigned long, unsigned short, unsigned char.)
"integer" and "int" mean very different things.

If unsigned int is 32 bits, then the hexadecimal representation you're
looking for is 8 F's, not 32 F's (0xFFFFFFFF).
But now my question is if
unsigned int i = -1;
would fail if the size of integer is not a multiple of 4 ?
It would fail to do what you're asking for, but I think that what
you're asking for isn't really what you need.

Suppose unsigned int is 18 bits (unlikely, but legal). Then the
largest value (UINT_MAX) of type unsigned int is 0x3FFFF, a '3'
followed by 4 'F's. 5 F's, 0xFFFFF, won't fit in an unsigned int, and
4 F's, 0xFFFF, is 0x0FFFF, so it's not really all F's. Unless you
don't care about leading zeros, but then 0xF, 0xFF, 0xFFF, and 0xFFFF
would all qualify as "all F's", and I don't think that's what you
want.

If you have a variable of type 'unsigned int' used to store "the state
of some object" (whatever that happens to mean), it would make a lot
more sense to use the largest possible value of type 'unsigned int' to
denote an invalid state. If that's consistent with your actual
requirements, then

unsigned int i = -1;

will work perfectly; it's guaranteed to set i to te largest possible
value of type 'unsigned int', and it will continue to work if you
later change from 'unsigned int' to, say, 'unsigned long'.

Does that answer your question? (If so, this talk of "all F's" has
been a waste of time.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Aug 30 '07 #33

P: n/a
Jack Klein wrote:
CBFalconer <cb********@yahoo.comwrote in comp.lang.c:
>pete wrote:
>>Ark Khasin wrote:
ju**********@yahoo.co.in 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 ?

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

So does unsigned int i = -1;

Not on 1's complement or sign-magnitude machinery.

Chuck, you know better than that.
Yup, looks like I goofed. Starting a new perfection record run.

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

Aug 30 '07 #34

P: n/a
On Aug 30, 8:30 am, Keith Thompson <ks...@mib.orgwrote:
"junky_fel...@yahoo.co.in" <junky_fel...@yahoo.co.inwrites:
On Aug 30, 3:24 am, Keith Thompson <ks...@mib.orgwrote:
"junky_fel...@yahoo.co.in" <junky_fel...@yahoo.co.inwrites:
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?
This variable is used to store the state of some object. The value all
F's is used as an Invalid state. So, motive is to initialize it with
the invalid state.

*Why* is "all F's" used to indicate an invalid state? Where did this
requirement come from? Is the requirement actually to use "all F's",
or is that your interpretation?
On my machine, unsigned integer is 32 bits. So, my requirement is to
initialize it with all 32 F's.

Please be precise; it's important. There are several "unsigned
integer" types. One of them is "unsigned int". (Some other unsigned
integer types are unsigned long, unsigned short, unsigned char.)
"integer" and "int" mean very different things.

If unsigned int is 32 bits, then the hexadecimal representation you're
looking for is 8 F's, not 32 F's (0xFFFFFFFF).
But now my question is if
unsigned int i = -1;
would fail if the size of integer is not a multiple of 4 ?

It would fail to do what you're asking for, but I think that what
you're asking for isn't really what you need.

Suppose unsigned int is 18 bits (unlikely, but legal). Then the
largest value (UINT_MAX) of type unsigned int is 0x3FFFF, a '3'
followed by 4 'F's. 5 F's, 0xFFFFF, won't fit in an unsigned int, and
4 F's, 0xFFFF, is 0x0FFFF, so it's not really all F's. Unless you
don't care about leading zeros, but then 0xF, 0xFF, 0xFFF, and 0xFFFF
would all qualify as "all F's", and I don't think that's what you
want.

If you have a variable of type 'unsigned int' used to store "the state
of some object" (whatever that happens to mean), it would make a lot
more sense to use the largest possible value of type 'unsigned int' to
denote an invalid state. If that's consistent with your actual
requirements, then

unsigned int i = -1;

will work perfectly; it's guaranteed to set i to te largest possible
value of type 'unsigned int', and it will continue to work if you
later change from 'unsigned int' to, say, 'unsigned long'.

Does that answer your question? (If so, this talk of "all F's" has
been a waste of time.)
Thanks Keith for your answer. It was my mistake. I shouldn't have used
all F's in my original question.

Aug 30 '07 #35

P: n/a
CBFalconer <cb********@yahoo.comwrote:
Jack Klein wrote:
CBFalconer <cb********@yahoo.comwrote in comp.lang.c:
pete wrote:
Ark Khasin wrote:
ju**********@yahoo.co.in 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 ?

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

So does unsigned int i = -1;

Not on 1's complement or sign-magnitude machinery.
Chuck, you know better than that.

Yup, looks like I goofed. Starting a new perfection record run.
Don't feel too bad... I almost posted the same reply before I caught
that it's only valid for signed types.

Richard
Aug 30 '07 #36

P: n/a
Richard Bos wrote:
CBFalconer <cb********@yahoo.comwrote:
>Jack Klein wrote:
>>CBFalconer <cb********@yahoo.comwrote:
.... snip ...
>>>>
Not on 1's complement or sign-magnitude machinery.

Chuck, you know better than that.

Yup, looks like I goofed. Starting a new perfection record run.

Don't feel too bad... I almost posted the same reply before I
caught that it's only valid for signed types.
I have been inconsolable for days now. Just hiding in a corner and
beating myself with a cat-o-nine-tails. The indescribable agony I
am suffering!

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

Aug 30 '07 #37

P: n/a
On Wed, 29 Aug 2007 23:29:09 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
In article <77***************************@news.hispeed.ch>,
Pietro Cerutti <ga**@gahr.chwrote:
Could you name an integer type whose width (in bits) sn't a multiple of 4?

Yes. DEC made several machines (such as the TOPS series) with 18 bit
int. The Honeywell L6 series used a 36 bit word; I do not recall
at the moment if it was an 18 or 36 bit int on the compilers.
Nits: DEC made PDP-6 and PDP-10 _machines_ with a 36-bit word and ALU,
and extensive 18-bit (halfword) _move_ operations but not arithmetic;
but addresses were (also) 18-bits so you could use address arithmetic
for some limited things -- as on many other architectures.
They also had hardware support for byte operations of any size from 1
to 36-bits, and are usually trotted out (along with GE/HIS and maybe
CDC) as one of the real (and once important) cases of non-8-bit (and
non-power-of-two-bit) bytes.

The TOPS-10 and TOPS-20 _operating systems_ ran on PDP-10 machines,
labelled as DECsystem 10 and DECsystem 20, but so did several other
(in-house/third-party) operating systems.

"PDP" (Programmed Data Processor) was the name for all of
classic-DEC's product line, until VAX. According to legend, the name
was orginally a way for customers to evade corporate/bureaucratic
restrictions on buying and operating things called "computers". The
other PDP-n machines were architecturally dissimilar, although PDP-7
and -9 (and I am told -4) did have 18-bit word.

I don't think there was ever a PDP-10 C compiler from DEC; in those
days they were strongly (even zealously) pushing BLISS instead for the
niche where C competed and eventually won. (By the later days of
PDP-11, and early VAX, DEC had con-C-ded. <G>) There may well have
been a DECUS (customer-contributed) one.

You could make reasonable arguments for SI18L36 or S18IL36. Or even a
compiler option to choose between them. You need P36 (well, actually
28 and probably 33) for char and void pointers; in theory you could
use 18 or maybe 19 for everything bigger, but it would be far too much
hassle to make use of the "saved" space, as well as breaking (code
using) the widespread though unjustified assumption that all C (data)
pointers are (approximately) the same, so I doubt anyone would bother.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Sep 9 '07 #38

P: n/a
David Thompson <da************@verizon.netwrites:
I don't think there was ever a PDP-10 C compiler from DEC; in those
days they were strongly (even zealously) pushing BLISS instead for the
niche where C competed and eventually won. (By the later days of
PDP-11, and early VAX, DEC had con-C-ded. <G>) There may well have
been a DECUS (customer-contributed) one.
There has been a port of gcc done in the 2000 (http://pdp10.nocrew.org/),
and kcc (available at the same place) was older and I think mostly standard
conformant, at least when invoked with the correct options.

Yours,

--
Jean-Marc
Sep 10 '07 #39

This discussion thread is closed

Replies have been disabled for this discussion.