Velocity Reviews > operators and type promotion

# operators and type promotion

swansnow
Guest
Posts: n/a

 12-12-2007
This is perhaps a basic question, but it's been a while since I've
done C, and none of the searches I did on operators and data types
wanted to address this, so here goes...

In the glibc (linux) library, I found this expression , and I want to
know exactly what it's doing.

res >= 0xfffff001u

Here, res is an int. It looks to me like res is being interpreted as
an unsigned int (long?) and then being compared against the constant
(-4095). So if res happens to be, say, -22, this expression will be
true. If res happens to be -4096, this statement will be false, maybe?

Is that right?

-Corinna

James Kuyper
Guest
Posts: n/a

 12-12-2007
swansnow wrote:
> This is perhaps a basic question, but it's been a while since I've
> done C, and none of the searches I did on operators and data types
> wanted to address this, so here goes...
>
> In the glibc (linux) library, I found this expression , and I want to
> know exactly what it's doing.
>
> res >= 0xfffff001u
>
> Here, res is an int. It looks to me like res is being interpreted as
> an unsigned int (long?) and then being compared against the constant
> (-4095). So if res happens to be, say, -22, this expression will be
> true. If res happens to be -4096, this statement will be false, maybe?
>
> Is that right?

It depends very much upon the value of INT_MAX and UINT_MAX. If INT_MAX
> 0xfffff001u, then the constant will be promoted to an int before

performing the comparison. As a result, the comparison will be done as a
signed comparison, and only very large positive values of res will pass
the test. If, on the other hand, INT_MAX < 0xfffff001u, then res will be
converted to the same type as the constant (which will be either
unsigned int or unsigned long, depending upon whether or not UINT_MAX >
0xfffff01u). The exact result you get will depend upon the results of
that conversion.

Peter Nilsson
Guest
Posts: n/a

 12-12-2007
James Kuyper <(E-Mail Removed)> wrote:
> swansnow wrote:
> > In the glibc (linux) library, I found this expression ,
> > and I want to know exactly what it's doing.
> >
> > res >= 0xfffff001u
> >
> > Here, res is an int. It looks to me like res is being
> > interpreted as an unsigned int (long?)

If 0xfffff001u <= UINT_MAX, it's equivalent to...

(unsigned) res >= 0xfffff001u

Otherwise it's equivalent to...

(unsigned long) res >= 0xfffff001u

> > and then being compared against the constant (-4095).
> > So if res happens to be, say, -22, this expression will
> > be true. If res happens to be -4096, this statement
> > will be false, maybe?
> >
> > Is that right?

>
> It depends very much upon the value of INT_MAX and
> UINT_MAX.

I think you've misremembered.

> If INT_MAX > 0xfffff001u, then the constant will be
> promoted to an int before performing the comparison.

Not according to 6.4.4.1p5. The u suffix means it is
an unsigned integer; at least an unsigned int, but
possibly a wider (higher rank). It's certainly not a
lower rank than unsigned int though. E.g. (1 > -1u)
is never true.

--
Peter

James Kuyper
Guest
Posts: n/a

 12-12-2007
Peter Nilsson wrote:
....
> I think you've misremembered.

Yes, you're right. I actually thought to myself "I'd better double check
before I post", and then decided not to.

swansnow
Guest
Posts: n/a

 12-12-2007
Thanks for the clarification. I was starting to worry...

On Dec 11, 6:24 pm, Peter Nilsson <(E-Mail Removed)> wrote:
> If 0xfffff001u <= UINT_MAX, it's equivalent to...
>
> (unsigned) res >= 0xfffff001u
>
> Otherwise it's equivalent to...
>
> (unsigned long) res >= 0xfffff001u
>