initializing an int variable with all F's

P

pete

Malcolm said:
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.
 
W

Walter Roberson

Pietro Cerutti said:
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.
 
K

Keith Thompson

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

pete

Keith said:
_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.
 
C

CBFalconer

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

CBFalconer

Harald said:
CBFalconer wrote:
.... snip ...


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

I'm sloppy. I don't really differentiate between undefined and
implementation defined, etc. I just try to avoid them.
 
C

CBFalconer

Pietro said:
.... 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.
 
K

Keith Thompson

CBFalconer said:
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.
 
J

Jack Klein

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

Chuck, you know better than that.

Initializing or assigning -1 to an unsigned type results in the usual
unsigned conversion for values out of range. This is defined as the
equivalent of the quantity (UTYPE_MAX + 1) being added or subtracted
until the value is within the range. And this is based on value, not
representation.

So initializing or assigning a value of -1 to:

unsigned char results in (unsigned char)((UCHAR_MAX + 1) - 1)

unsigned int results in (unsigned int)((UINT_MAX + 1) - 1)

....I could go on, but I think the pattern is obvious.

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

Jack Klein

<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

The reason for using -1 instead of UINT_MAX is to allow for the case
when you, or a later maintainer, needs to change the unsigned int to
unsigned long, or unsigned long long. They must remember to hunt down
and change each and every place where a value is assigned or
initialized with UINT_MAX and change it to ULONG_MAX or ULLONG_MAX.
Miss one, and ka-boom!

Even if unsigned int is always large enough on the current platform,
this could happen if the program is ported to a platform where
UINT_MAX is less, requiring a change to ULONG_MAX.

The other reason is that you sometimes need to initialize opaque
unsigned types, that is those that don't directly have a _MAX in
<limits.h> (pre-C99), without looking up the type definition for the
type.

What do you replace:

size_t s = -1;

Could be UINT_MAX, ULONG_MAX, ULLONG_MAX, etc., on different
platforms.

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

Jack Klein

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
 
J

Jack Klein

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
 
J

junky_fellow

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

Keith Thompson

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.)
 
J

junky_fellow

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


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


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

Richard Bos

CBFalconer said:
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
 

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