initializing an int variable with all F's

P

Pietro Cerutti

Keith said:
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?
 
J

junky_fellow

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

Ark Khasin

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

Malcolm McLean

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

CBFalconer

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

ravi

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

Pietro Cerutti

Pietro said:
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);
}
 
P

pete

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

pete

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
 
P

Philip Potter

Malcolm said:
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
 
P

pemo

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
 
P

Philip Potter

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
 
K

Kenneth Brody

pete said:
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:[email protected]>
 
H

Harald van =?UTF-8?B?RMSzaw==?=

CBFalconer said:
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 <[email protected]>.
Do you disagree with it, or did you not read that message (yet)?
 
K

Keith Thompson

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

Old Wolf

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

pete

pemo said:
<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.
 
P

pete

Kenneth said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top