473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

operators and type promotion

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
Dec 12 '07 #1
4 1493
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.
Dec 12 '07 #2
James Kuyper <jameskuy...@verizon.netwrote:
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
Dec 12 '07 #3
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. :-(
Dec 12 '07 #4
Thanks for the clarification. I was starting to worry... :)

On Dec 11, 6:24 pm, Peter Nilsson <ai...@acay.com.auwrote:
If 0xfffff001u <= UINT_MAX, it's equivalent to...

(unsigned) res >= 0xfffff001u

Otherwise it's equivalent to...

(unsigned long) res >= 0xfffff001u
Dec 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Zenon | last post by:
Folks, I am having a terrible time overloading operators. I have tried what I thought was the correct way, I tried the cheating (friend declarations), all to no avail. Sorry for posting tons of...
8
by: BigMan | last post by:
Can someone cite the rules for type promotion in C++? And, in particular, what is the type of the result of adding 2 values of type char?
1
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a...
1
by: dogbert1793 | last post by:
Consider the code: unsigned char uc; cout << "sizeof( unsigned char ) = " << sizeof( uc ) << endl; cout << "sizeof( ~unsigned char ) = " << sizeof( ~uc ) << endl; Why is the output: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.