473,231 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

initializing an int variable with all F's

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

<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
"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
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
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
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
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
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
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
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
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
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
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
"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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
14
by: Avi Uziel | last post by:
Hi All, I'm writing a Windows DLL which contain some utility classes. One of my classes is Singleton, therefore contain some static members. I'm using VC6 and linking to the DLL statically. ...
4
by: hrmadhu | last post by:
Hi, I wish to declare a vector of deque of int, which I do as follows. #include<vector> #include<deque> #include<iostream> using namespace std; int main(int argc, char* argv) {
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
16
by: Ray | last post by:
Hi all, After many years of C, I thought I'd move to C++ recently. I think because I think in C, I'm coming to a misunderstanding of something. I've created a class foo which contains a...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.