Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > operators and type promotion

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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.
 
Reply With Quote
 
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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implicit promotion of chars by bitwise operators jonathan@sliid.org C++ 1 04-12-2009 10:25 AM
Integer promotion, relational operators, and unequal integer ranks. charles_gero@merck.com C Programming 1 01-15-2007 11:21 PM
Problem about type promotion and equality operator. KOFKS C Programming 2 06-06-2006 10:32 AM
Data Recovery Book and Online Business Promotion, Products Sales Promotion, Search Engine Optimization and Online Data Recovery Training services Author Tarun Tyagi Cisco 0 12-15-2004 01:52 AM
The promotion code "MSUU4C8E3475" is not a valid promotion code Sam-Hong Kong MCDST 2 03-04-2004 06:47 PM



Advertisments