473,395 Members | 1,679 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,395 software developers and data experts.

unsigned == signed ?

HH
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done? I also wanted to know if we can assign a number to
uint & int variables such that "unit var != int var". Thanks so much
Nov 14 '05 #1
7 1738
"HH" <HH@earthlink.net> wrote in
news:1c******************@newsread1.news.pas.earth link.net:
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for
the equality test?
I would say an unsigned comparison is being done. I believe that y will be
promoted by C to unsigned int (which I assume uint is typedef'd to be)
before doing the comparison so they should be equal. You should have
gotten a warning that you are comparing signed with unsiged types - a clue
to not do that.
If it doesn't can you give me an example that shows
bit comparison isn't done? I also wanted to know if we can assign a
number to uint & int variables such that "unit var != int var". Thanks
so much


To compare values you must use reasonable types, there is no such thing as
an unsiged -1 so how can you expect a meaningful comparison. You can
always compare int to int and unsigned to unsigned but when comparing
unsigned to signed it simply may not make sense.

--
- Mark ->
--
Nov 14 '05 #2
Greetings,

HH wrote:
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done? I also wanted to know if we can assign a number to
uint & int variables such that "unit var != int var". Thanks so much


Someone will likely complain about the lack of type uint in C as well as
the undefined behaviour caused by not including stdio.h, as well as the
possible lack of output due to not newline at the end of the printf().

The answer, however, assuming uint to be equivalent to unsigned int, is
that y is promoted to unsigned int before the comparison courtesy of
section 6.3.1.8 : usual arithmetic conversion and these two values are
indeed equal.
--
Kyle A. York
Sr. Subordinate Grunt
DSBU

Nov 14 '05 #3
HH wrote:
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL".
Well, no: This code doesn't compile, because `uint'
has not been declared. (There are other problems, too,
but that's not the point.) I'll answer on the assumption
that `uint' is a typedef'ed or #define'd alias for
`unsigned int'.
Does this mean that bit comparison is done for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done?
No and no. C's comparison operators are all defined
as working with the values of their operands, not the
representations thereof. What you have discovered is
that (1) when an `unsigned int' and an `int' are compared,
the `int' value is first converted to `unsigned int', and
(2) `(unsigned int)-1 == (unsigned int)-1'.
I also wanted to know if we can assign a number to
uint & int variables such that "unit var != int var". Thanks so much


Assuming that `unit' is also an alias for `unsigned
int', it is possible for this to happen -- but only under
a rather "exotic" set of conditions. These conditions are
permitted by the C language Standard, but never ("What,
never?" "Well, hardly ever.") seen in practice:

1) The number must be in the allowable range for an
`unsigned int' but out of range for an `int', that
is, `INT_MAX < number && number <= UINT_MAX'.

2) The attempt to convert this too-large value to an
`int' produces an implementation-defined result
instead of raising an implementation-defined signal.

3) The implementation-defined result is a valid `int'
value with the interesting property that converting
it to `unsigned int' does not yield the original
number.

This chain of events is unlikely, but permitted by the
Standard -- converting a value from `unsigned int' to `int'
and back again is required to be the identity operation only
if the value is in the legitimate range of an `int'. If it's
not, the Standard permits other things to happen. It's more
a theoretical possibility than anything else, though.

Here's the big question: Why do you care? What are you
trying to do?

--
Er*********@sun.com

Nov 14 '05 #4

"HH" <HH@earthlink.net> wrote in message
news:1c******************@newsread1.news.pas.earth link.net...
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for the equality test? If it doesn't can you give me an example that shows bit
comparison isn't done? no ! unsigned int, and signed int are very different the way they are
represented in bits. therefore, when you to compare the 2 different types
(like comparing apples and oranges) one of them has to be converted, in this
case, the 'int' is converted to a 'uint', and becomes 0, since unsigned
numbers cannot be less then zero.
and besides, the == operator it *not* a bit comparison operator, it is a
variable comparison operator, if you want to do bit comparison you use the
bitwise operators, &,|,^,~. (and, or, exclusive or, not. respectively)
I also wanted to know if we can assign a number to
unit & int variables such that "unit var != int var". Thanks so much yes, if unit var = 1, and int var is any value other than 1 (-1, 0, -10, 10,
100, whatever) then they will be 'not equal' if you use the operator '!='
for example:

uint x = 1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");

will print NOT EQUAL since y is converted to an unsigned int and becomes 0

Nov 14 '05 #5
On 6 Aug 2004 15:52:38 GMT, "Mark A. Odell" <od*******@hotmail.com>
wrote in comp.lang.c:
"HH" <HH@earthlink.net> wrote in
news:1c******************@newsread1.news.pas.earth link.net:
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for
the equality test?


I would say an unsigned comparison is being done. I believe that y will be
promoted by C to unsigned int (which I assume uint is typedef'd to be)
before doing the comparison so they should be equal. You should have
gotten a warning that you are comparing signed with unsiged types - a clue
to not do that.
If it doesn't can you give me an example that shows
bit comparison isn't done? I also wanted to know if we can assign a
number to uint & int variables such that "unit var != int var". Thanks
so much


To compare values you must use reasonable types, there is no such thing as
an unsiged -1 so how can you expect a meaningful comparison. You can
always compare int to int and unsigned to unsigned but when comparing
unsigned to signed it simply may not make sense.


Beg to disagree. There is indeed such a thing as unsigned -1. C
defines both initializations and assignments in terms of values.

Initializing any unsigned integer type with the value of -1, or
assigning a value of -1 to any unsigned integer type, follows the
usual unsigned rules of modulo wrap around.

UTYPE x = -1;

....is guaranteed to produce the value UTYPE_MAX in x.

Given:

int si = -1;
unsigned int ui = -1;

....then the comparison:

if (si == ui)

....is guaranteed to be true. If the implementation uses a non-2's
complement representation for negative signed integer types, the bit
patterns of ui and si will be different, but the comparison is also
based on *value*, not on bit representation.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
On Sat, 7 Aug 2004 08:27:30 +0800, "someone else"
<zo****@fuck.the.spammers.net> wrote in comp.lang.c:

"HH" <HH@earthlink.net> wrote in message
news:1c******************@newsread1.news.pas.earth link.net...
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done?

no ! unsigned int, and signed int are very different the way they are
represented in bits. therefore, when you to compare the 2 different types
(like comparing apples and oranges) one of them has to be converted, in this
case, the 'int' is converted to a 'uint', and becomes 0, since unsigned
numbers cannot be less then zero.


This is totally incorrect. Assigning or initializing any unsigned
integer type with the value -1 is required and guaranteed to produce
the maximum value of the unsigned type. Indeed no unsigned type can
hold the value -1, but the conversion is completely and specifically
defined.
and besides, the == operator it *not* a bit comparison operator, it is a
variable comparison operator, if you want to do bit comparison you use the
bitwise operators, &,|,^,~. (and, or, exclusive or, not. respectively)
I also wanted to know if we can assign a number to
unit & int variables such that "unit var != int var". Thanks so much

yes, if unit var = 1, and int var is any value other than 1 (-1, 0, -10, 10,
100, whatever) then they will be 'not equal' if you use the operator '!='
for example:


You completely misunderstood this part of the OP's question.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:1k********************************@4ax.com...
On Sat, 7 Aug 2004 08:27:30 +0800, "someone else"
<zo****@fuck.the.spammers.net> wrote in comp.lang.c:

"HH" <HH@earthlink.net> wrote in message
news:1c******************@newsread1.news.pas.earth link.net...
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done
for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done? no ! unsigned int, and signed int are very different the way they are
represented in bits. therefore, when you to compare the 2 different types (like comparing apples and oranges) one of them has to be converted, in this case, the 'int' is converted to a 'uint', and becomes 0, since unsigned
numbers cannot be less then zero.


This is totally incorrect. Assigning or initializing any unsigned
integer type with the value -1 is required and guaranteed to produce
the maximum value of the unsigned type. Indeed no unsigned type can
hold the value -1, but the conversion is completely and specifically
defined.

true, my mistake.
however the values are converted first. to prove it, try this
long x = -1; /* (32 bit, signed) */
unit y = -1; /* (16 bit, unsigned) */
if (x == y) printf("EQUAL");
will print EQUAL !
and besides, the == operator it *not* a bit comparison operator, it is a
variable comparison operator, if you want to do bit comparison you use the bitwise operators, &,|,^,~. (and, or, exclusive or, not. respectively)
I also wanted to know if we can assign a number to
unit & int variables such that "unit var != int var". Thanks so much yes, if unit var = 1, and int var is any value other than 1 (-1, 0, -10, 10, 100, whatever) then they will be 'not equal' if you use the operator '!=' for example:


You completely misunderstood this part of the OP's question.

again, you are correct.
but it's too bad that my personal beliefs do not allow me to respond... (I
oppose rudeness)
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #8

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

Similar topics

3
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like ...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
20
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
4
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it...
3
by: Nicholas Zhou | last post by:
Hi, I was writing a testing program to test the ranges of char, short, int and long variables on my computer, both signed and unsigned. Everything was fine except for unsigned int and unsigned...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.