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.
Malcolm said:It is one of those awkward things.
In practise
you are never likely to program a non-two's complement machine.
Pietro Cerutti said:Could you name an integer type whose width (in bits) sn't a multiple of 4?
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?
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).
pete said:Keith Thompson wrote:
sizeof(int) == 2
pete said:So does unsigned int i = -1;
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?)
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)?
ravi said:Another way is :-
int i = 0xFFFF;
Pietro said:.... snip ...
Could you name an integer type whose width (in bits) sn't a
multiple of 4?
CBFalconer said:Not on 1's complement or sign-magnitude machinery.
Not on 1's complement or sign-magnitude machinery.
<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
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);
}
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.)
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?
[email protected] said: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 ?
Jack said:Chuck, you know better than that.
Thanks Keith for your answer. It was my mistake. I shouldn't have used*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.)
CBFalconer said:Yup, looks like I goofed. Starting a new perfection record run.
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.