Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > [newbie]value of a in "unsigned int a=-16"

Reply
Thread Tools

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

 
 
iceBlue
Guest
Posts: n/a
 
      07-03-2003
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-
-----------------------------------------------------------------
Life is far too important a thing ever to talk seriously about.


 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      07-03-2003


"Arthur J. O'Dwyer" wrote:
>
> On Thu, 3 Jul 2003, iceBlue wrote:
> >
> > Hello
> >
> > I have this small doubt on unsigned data.

>
> 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
 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      07-04-2003
"Arthur J. O'Dwyer" <(E-Mail Removed)> wrote:
> 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.

--
Simon.


 
Reply With Quote
 
Brett Frankenberger
Guest
Posts: n/a
 
      07-04-2003
In article <(E-Mail Removed)>,
Arthur J. O'Dwyer <(E-Mail Removed)> wrote:
>
>> unsigned int a=-16;

>
>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
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      07-07-2003


"Arthur J. O'Dwyer" wrote:

> > > First of all, what you have is a "question." "Doubt" is a verb,


> > Is English *your* first language?
> >
> > Main Entry: 2doubt
> > Function: noun


> 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
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      07-07-2003


Don Porges wrote:
>
> "Mark McIntyre" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> > On Thu, 3 Jul 2003 14:01:01 -0400 (EDT), in comp.lang.c , "Arthur J.
> > O'Dwyer" <(E-Mail Removed)> wrote:


> > Well, actually doubt is a noun too. But it means a disbelief. And I
> > have no doubt that this is not what the OP means.


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
 
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
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments