Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > unsigned == signed ?

Reply
Thread Tools

unsigned == signed ?

 
 
Raj Pashwar
Guest
Posts: n/a
 
      10-11-2011
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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-11-2011
Raj Pashwar <(E-Mail Removed)> writes:
> 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


There is no predefined type "uint" in C. I'll assume you meant
"unsigned int".

Here's a corrected version of your program:

#include <stdio.h>

int main(void)
{
unsigned int x = -1;
int y = -1;
if (x == y) {
printf("EQUAL\n");
}
else {
printf("NOT EQUAL\n");
}
return 0;
}

There's actually a fair amount of stuff going on here, some of it
not obvious.

The expression "-1" is of type int. The initialization applies an
implicit conversion from int to unsigned int. The rules for such
a conversion (see C99 6.3.1.3p2) mean that the initial value of
x is UINT_MAX. (This is probably 4294967295 on a typical 32-bit
system, but other values are possible.)

The comparison "x == y" is comparing an unsigned int value to a
(signed) int value. Before the comparison is performed, the
"usual arithmetic conversions" are applied to the operands (C99
6.5.9p4, 6.3.1.. In this case, the int operand is converted to
unsigned int. Since y's value is -1, the result is the same as
the result of the conversion in x's initialization.

The result is that "x == y" is comparing UINT_MAX to UINT_MAX,
which of course yields a true value (1).

As it happens, the result is the same that you'd get if the program
just performed bitwise comparisons of the operands. The rules
for conversions and comparisons are defined purely in terms of
the arithmetic *values* of the operands, but the rules are written
in such a way that they can be implemented easily on most systems
(2's-complement representation for signed values).

The latest draft of the C standard is at
http://www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf
(that's a 3.7-megabyte PDF document).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      10-11-2011
On 10/11/2011 03:43 PM, Raj Pashwar wrote:
> int main()
> {
> uint x = -1;


uint is presumably a typedef; it's not a typedef provided by the C
standard itself, so you should have let us know what type it is a
typedef for. I'll presume "unsigned int".

> 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?


No, it means that precisely the same conversion is performed on -1 when
it is used to initialize x, as is performed on the value of y when it is
converted to the type of 'x' for the comparison. Assuming that uint is a
typedef for unsigned int, the result is guaranteed to be UINT_MAX in
both cases.

> ... If it doesn't can you give me an example that shows bit
> comparison isn't done?


Yes. Change 'y' to signed char, and x to unsigned long. If sizeof(signed
char) < sizeof(unsigned long) (which is almost, but not quite, certainly
guaranteed), most of the bits that are set in x won't have any
comparable bits in 'y'. However, the values will still compare equal,
because y's value of -1 will be converted to ULONG_MAX before the
conversion.

In the unlikely event that you can locate an implementation where 'int'
uses a 1's complement or sign-magnitude representation, you'll find that
you still get exactly the same result, which ALSO proves that it's a
value comparison, not a bit comparison. That's because x and y will have
very different bit patterns on such a system.

> ... I also wanted to know if we can assign a number to
> uint & int variables such that "unit var != int var". Thanks so much


Not as long as the number is within the range [INT_MIN, INT_MAX].
Because the same conversions are performed in both cases, the values
necessarily compare equal.
 
Reply With Quote
 
Quentin Pope
Guest
Posts: n/a
 
      10-11-2011
On Tue, 11 Oct 2011 19:43:03 +0000, Raj Pashwar 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?


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 uint & 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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-11-2011
On 10/11/2011 04:33 PM, Quentin Pope wrote:
> On Tue, 11 Oct 2011 19:43:03 +0000, Raj Pashwar wrote:

....
>> I also wanted to know if we can assign a number
>> to uint & 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;


You've not using the same initializer for x and for y; I believe that's
what he meant when he referred to assigning "a number to" both variables.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-11-2011
Quentin Pope <(E-Mail Removed)> writes:
> On Tue, 11 Oct 2011 19:43:03 +0000, Raj Pashwar 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?

>
> no ! unsigned int, and signed int are very different the way they are
> represented in bits.


They're not all that different. In fact, in the absence of padding bits
and/or trap representations, the language guarantees that a value that's
representable as int and unsigned int (i.e., in the range 0 .. INT_MAX)
has the same representation in both types.

> 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.


It's true that one of them is converted. This is because the language
standard says so; it's not a consequence of their representations. The
language *could* have defined "==" as a bitwise comparison (and in fact
that would have yielded the same results for int vs. unsigned int
comparisonms on 2's-complement systems).

But you're mistaken about the semantics of the conversion. When an
integer value is converted to an unsigned type, and the value cannot be
represented in the target type, " the value is converted by repeatedly
adding or subtracting one more than the maximum value that can be
represented in the new type until the value is in the range of the new
type" (C99 6.3.1.3p2). In this particular case, convering the int value
-1 to unsigned int yields UINT_MAX.

> 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)


It's more accurate to say that it's a *value* comparison operator; there
needn't be any variables involved (consider "1 + 1 == 2").

The bitwise operators don't do comparisons. If you want to do
a bitwise equality comparison, the memcmp() function is probably
the best approach. Or you could treat the objects as arrays of
unsigned char and compare the element values (that's basically what
memcmp() does).

>> I also wanted to know if we can assign a number
>> to uint & 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


No, it will print NOT EQUAL because y is converted to an unsigned int
and becomes UINT_MAX (typically 4294967295), which is not equal to 1.
You'd get the same result if "==" did a bitwise comparison.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-11-2011
Quentin Pope <(E-Mail Removed)> writes:

> On Tue, 11 Oct 2011 19:43:03 +0000, Raj Pashwar 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?

>
> 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.


Not zero, but UINT_MAX.

> 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 uint & 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


See above!

--
Ben.
 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      10-12-2011
Quentin Pope wrote:
> On Tue, 11 Oct 2011 19:43:03 +0000, Raj Pashwar 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?

>
> 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.


Is this meant to be a joke? Or is -1 equal to 0 where you come from?

> ...

 
Reply With Quote
 
Quentin Pope
Guest
Posts: n/a
 
      10-12-2011
On Tue, 11 Oct 2011 14:34:29 -0700, Keith Thompson wrote:

> Quentin Pope <(E-Mail Removed)> writes:
>> On Tue, 11 Oct 2011 19:43:03 +0000, Raj Pashwar 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?

>>
>> no ! unsigned int, and signed int are very different the way they are
>> represented in bits.

>
> They're not all that different. In fact, in the absence of padding bits
> and/or trap representations, the language guarantees that a value that's
> representable as int and unsigned int (i.e., in the range 0 .. INT_MAX)
> has the same representation in both types.
>
>> 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.

>
> It's true that one of them is converted. This is because the language
> standard says so; it's not a consequence of their representations. The
> language *could* have defined "==" as a bitwise comparison (and in fact
> that would have yielded the same results for int vs. unsigned int
> comparisonms on 2's-complement systems).
>
> But you're mistaken about the semantics of the conversion. When an
> integer value is converted to an unsigned type, and the value cannot be
> represented in the target type, " the value is converted by repeatedly
> adding or subtracting one more than the maximum value that can be
> represented in the new type until the value is in the range of the new
> type" (C99 6.3.1.3p2). In this particular case, convering the int value
> -1 to unsigned int yields UINT_MAX.
>
>> 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)

>
> It's more accurate to say that it's a *value* comparison operator; there
> needn't be any variables involved (consider "1 + 1 == 2").
>
> The bitwise operators don't do comparisons. If you want to do a bitwise
> equality comparison, the memcmp() function is probably the best
> approach. Or you could treat the objects as arrays of unsigned char and
> compare the element values (that's basically what memcmp() does).
>
>>> I also wanted to know if we can assign a number to uint & 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

>
> No, it will print NOT EQUAL because y is converted to an unsigned int
> and becomes UINT_MAX (typically 4294967295), which is not equal to 1.
> You'd get the same result if "==" did a bitwise comparison.


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 !
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-12-2011
On 10/12/2011 12:04 PM, Quentin Pope wrote:
....
> 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) */


uint?

> if (x == y) printf("EQUAL");
> will print EQUAL !


Of course, and that's fully consistent with what Keith said.
 
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
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Convert a signed binary number into a signed one ? Rob1bureau VHDL 1 02-27-2010 12:13 AM
signed(12 downto 0) to signed (8 downto 0) kyrpa83 VHDL 1 10-17-2007 06:58 PM
signed to unsigned Patrick VHDL 1 06-07-2004 01:59 PM
STD_LOGIC_VECTOR vs. UNSIGNED vs. SIGNED Jeremy Pyle VHDL 3 06-28-2003 10:47 PM



Advertisments