[newbie]value of a in "unsigned int a=-16"

I

iceBlue

Hello

I have this small doubt on unsigned data.
In the following code snippet:

unsigned int a=-16;
int b=-15;
if(b>a) printf("b>a");
else printf("a>b");

Iam getting b>a as the answer.Why?
With unsigned data,what i thought was that,they dont have to reserve the
first bit for sign.Then how come the value of a is -16 even when its
unsigned?

Thanks in adavance..

-iceBlue-
 
D

Default User

Arthur J. O'Dwyer said:
First of all, what you have is a "question." "Doubt" is a verb,
meaning "to be unsure of the truth of." As in, "I doubt that
English is your first language."

Is English *your* first language?

Main Entry: 2doubt
Function: noun
Date: 13th century
1 a : uncertainty of belief or opinion that often interferes with
decision-making b : a deliberate suspension of judgment
2 : a state of affairs giving rise to uncertainty, hesitation, or
suspense
3 a : a lack of confidence : DISTRUST b : an inclination not to believe
or accept
synonym see UNCERTAINTY





Brian Rodenborn
 
S

Simon Biber

Arthur J. O'Dwyer said:
Since -16 cannot be represented as an unsigned int, the
compiler implicitly converts -16 to an unsigned value by
repeated addition of UINT_MAX. Thus, after the
initialization, a == UINT_MAX-16.

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned a = -16;
printf("UINT_MAX == %u\n", UINT_MAX);
printf("UINT_MAX - 15 == %u\n", UINT_MAX - 15);
printf("UINT_MAX - 16 == %u\n", UINT_MAX - 16);
printf("a == %u\n", a);
return 0;
}

C:\prog\c>gcc -std=c99 -pedantic -Wall -W -O2 unsigned.c -o unsigned
C:\prog\c>unsigned
UINT_MAX == 4294967295
UINT_MAX - 15 == 4294967280
UINT_MAX - 16 == 4294967279
a == 4294967280

The compiler converts -16 to an unsigned value by repeated
addition of UINT_MAX + 1. Thus, after the initialization,
a == UINT_MAX - 15.
Since a and b are of different types, one of them must be
promoted to the type of the other one. Since a is unsigned
and b is not, it is b that gets promoted. So the value
being compared to UINT_MAX-16 in this expression is
(unsigned int)b, or UINT_MAX-15.

No, it's comparing `a' which is UINT_MAX-15 to `(unsigned)b'
which is UINT_MAX-14.
 
B

Brett Frankenberger

Since -16 cannot be represented as an unsigned int, the compiler
implicitly converts -16 to an unsigned value by repeated addition
of UINT_MAX. Thus, after the initialization, a == UINT_MAX-16.

Actually, addition of (UINT_MAX+1).

- Brett
 
D

Default User

Yeah, yeah. I didn't bother to actually look up the word just
to correct the guy's usage. (But its *first* entry in the dictionary
is as a verb. So.)

Something has to be listed first, that doesn't make secondary listings
incorrect.

My point was that it's a poor idea to try and correct someone's grammar
on usenet. It would have been a fine idea to point out that,
idiomatically, the use of doubt there was not typical English.



Brian Rodenborn
 
D

Default User

That's untrue. Doubt can be used as the OP did, it just isn't a
*typical* usage.
I've noticed this error all over Usenet, all of a sudden -- maybe the last 3 months or so.
Has some new incorrect English-as-a-second-language textbook come out that's spreading
this?

It's not an error per se, just atypical. As to its frequency in some of
our non-native English-speaking friends, that I don't have a clue about.
It does seem to be rather common of late.




Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top