It is currently Sat May 25, 2013 2:35 pm

All times are UTC - 5 hours [ DST ]




 Page 4 of 5 [ 50 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next
#31) 
Author Message
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sun May 13, 2012 2:07 pm 
User avatar

Joined: Mon May 10, 2010 3:46 pm
Posts: 3864
Location: Earth
jaydip wrote:
First thing that comes to mind that my compiler may be faulty,how on earth can be "-1" greater than "1"?This is one of the very weird rules of C/C++. if u mix an unsigned and signed type the result is of the unsigned type.If u convert "-1" to a positive integer it becomes a huge number which is definitely greater than "1". The third "If...else" shows what is happening behind the scenes by means of a cast.Moral of the story ,never mix an unsigned and signed type.We tend to declare unsigned types for some variables which can't be negative for e.g. Age,but this is a bad choice.We should only declare unsigned types for bitfields (more on this later). Now cppcheck throws a waring when u compile this code,.

Ok, how does converting '-1' to a positive integer make it a huge number??? :lol: :scratch:



_________________
What is this thing right now?
Put your arms up on one side of the horizon, put them up into the sky and twist them across, meeting unto the other side of the horizon. That is a sign symbol of life.
Face the goodness in life.


Offline
 Profile  
 
#32) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Mon May 14, 2012 12:44 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1827
Location: India
BoFox wrote:
jaydip wrote:
First thing that comes to mind that my compiler may be faulty,how on earth can be "-1" greater than "1"?This is one of the very weird rules of C/C++. if u mix an unsigned and signed type the result is of the unsigned type.If u convert "-1" to a positive integer it becomes a huge number which is definitely greater than "1". The third "If...else" shows what is happening behind the scenes by means of a cast.Moral of the story ,never mix an unsigned and signed type.We tend to declare unsigned types for some variables which can't be negative for e.g. Age,but this is a bad choice.We should only declare unsigned types for bitfields (more on this later). Now cppcheck throws a waring when u compile this code,.

Ok, how does converting '-1' to a positive integer make it a huge number??? :lol: :scratch:

Ok lets find the solution shall we :grin:
For signed numbers the last digit carry the sign bit,"0" for +ve numbers ,"1" for -ve numbers.Now in case of unsigned numbers the last digit is not a sign bit any more,because sign bit has no meaning in case of unsigned numbers.So "-1" which is all "1"s represent a very large +ve integer.Is that clear now?Do u want me to add this into the original article?


Online
 Profile  
 
#33) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Mon May 14, 2012 2:24 am 
User avatar

Joined: Sat Apr 24, 2010 11:19 am
Posts: 5013
Fewer and fewer people get this sort of thing nowadays, because they have never dealt with assembly code and manipulated binary numbers.

C is pretty unforgiving about errors too. It's mostly up to the programmer not to make errors. This is why there are so many software bugs out there.



_________________
This is such total Horse-S**t!
"At NVIDIA we know that all shredders are green." --Jensen Huang
Adam knew he should have bought a PC, but Eve fell for the marketing hype. >:)
Offline
 Profile  
 
#34) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Mon May 14, 2012 2:38 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1827
Location: India
grstanford wrote:
Fewer and fewer people get this sort of thing nowadays, because they have never dealt with assembly code and manipulated binary numbers.

C is pretty unforgiving about errors too. It's mostly up to the programmer not to make errors. This is why there are so many software bugs out there.

I agree completely.Now a days unless u are doing systems programming it is very fool hardy to choose C/C++.There are many other languages which takes the burden away from the programmer and simplifies their job.But we have lots of legacy C/C++ code which needs maintaining unfortunately :grin: .Also if u are into CUDA C is the pretty obvious choice.


Online
 Profile  
 
#35) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Tue May 15, 2012 12:20 am 
User avatar

Joined: Mon May 10, 2010 3:46 pm
Posts: 3864
Location: Earth
jaydip wrote:
BoFox wrote:
jaydip wrote:
First thing that comes to mind that my compiler may be faulty,how on earth can be "-1" greater than "1"?This is one of the very weird rules of C/C++. if u mix an unsigned and signed type the result is of the unsigned type.If u convert "-1" to a positive integer it becomes a huge number which is definitely greater than "1". The third "If...else" shows what is happening behind the scenes by means of a cast.Moral of the story ,never mix an unsigned and signed type.We tend to declare unsigned types for some variables which can't be negative for e.g. Age,but this is a bad choice.We should only declare unsigned types for bitfields (more on this later). Now cppcheck throws a waring when u compile this code,.

Ok, how does converting '-1' to a positive integer make it a huge number??? :lol: :scratch:

Ok lets find the solution shall we :grin:
For signed numbers the last digit carry the sign bit,"0" for +ve numbers ,"1" for -ve numbers.Now in case of unsigned numbers the last digit is not a sign bit any more,because sign bit has no meaning in case of unsigned numbers.So "-1" which is all "1"s represent a very large +ve integer.Is that clear now?Do u want me to add this into the original article?

Not completely clear for me yet, sorry.. yeah, add it to the original article.. thanks!!

By the way, just a guess.. you're in Mumbai rather than Calcutta/Delhi/Hyderabad/etc..?



_________________
What is this thing right now?
Put your arms up on one side of the horizon, put them up into the sky and twist them across, meeting unto the other side of the horizon. That is a sign symbol of life.
Face the goodness in life.


Offline
 Profile  
 
#36) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Tue May 15, 2012 1:33 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1827
Location: India
BoFox wrote:
jaydip wrote:
BoFox wrote:
jaydip wrote:
First thing that comes to mind that my compiler may be faulty,how on earth can be "-1" greater than "1"?This is one of the very weird rules of C/C++. if u mix an unsigned and signed type the result is of the unsigned type.If u convert "-1" to a positive integer it becomes a huge number which is definitely greater than "1". The third "If...else" shows what is happening behind the scenes by means of a cast.Moral of the story ,never mix an unsigned and signed type.We tend to declare unsigned types for some variables which can't be negative for e.g. Age,but this is a bad choice.We should only declare unsigned types for bitfields (more on this later). Now cppcheck throws a waring when u compile this code,.

Ok, how does converting '-1' to a positive integer make it a huge number??? :lol: :scratch:

Ok lets find the solution shall we :grin:
For signed numbers the last digit carry the sign bit,"0" for +ve numbers ,"1" for -ve numbers.Now in case of unsigned numbers the last digit is not a sign bit any more,because sign bit has no meaning in case of unsigned numbers.So "-1" which is all "1"s represent a very large +ve integer.Is that clear now?Do u want me to add this into the original article?

Not completely clear for me yet, sorry.. yeah, add it to the original article.. thanks!!

By the way, just a guess.. you're in Mumbai rather than Calcutta/Delhi/Hyderabad/etc..?

OK sorry,I should have made it much clearer.I will explain it by using a Nibble(4 bits)
8 4 2 1
--------
0 0 0 0 ---> Decimal 0(Add all the digits under 8 4 2 1)
0 0 0 1 ---> Decimal +1
0 1 1 1 ---> Decimal +7
1 1 1 1 ---> (-7) in signed integer (+15) as an unsigned integer

Now u can see if we have all ones that represent a big +ve integer if we use an unsigned integer type.If u have any questions please let me know and thanks for asking me to clarify :good: .I am currently @ Bangalore.


Online
 Profile  
 
#37) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Mon May 21, 2012 5:50 am 
User avatar

Joined: Mon May 10, 2010 3:46 pm
Posts: 3864
Location: Earth
Bangalore.. ahh, cool! For some reason, I thought Mumbai was the tech hub of India, but I was wrong. From Wiki:
Quote:
Bangalore is well known as a hub for India's information technology sector. It is among the top 10 preferred entrepreneurial locations in the world.[8]

Today as a large city and growing metropolis, Bangalore is home to many well-recognized colleges and research institutions in India. Numerous public sector heavy industries, software companies, aerospace, telecommunications, and defence organisations are located in the city. Bangalore is known as the Silicon Valley of India because of its position as the nation's leading IT exporter.[9][10][11] A demographically diverse city, Bangalore is a major economic and cultural hub and the second fastest growing major metropolis in India.[12]

http://en.wikipedia.org/wiki/Bangalore



_________________
What is this thing right now?
Put your arms up on one side of the horizon, put them up into the sky and twist them across, meeting unto the other side of the horizon. That is a sign symbol of life.
Face the goodness in life.


Offline
 Profile  
 
#38) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Oct 27, 2012 9:24 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1827
Location: India
It has been a long time since I updated this and this was my fault for being too lazy :P.I was thinking about doing some neat stuff and then I thought why not CUDA?What do you guys think?


Online
 Profile  
 
#39) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Oct 27, 2012 9:27 am 
User avatar

Joined: Fri Jul 04, 2008 1:26 am
Posts: 19831
Location: 404 - Not Found!
Why not?

:)


Offline
 Profile  
 
#40) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Oct 27, 2012 10:22 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1827
Location: India
Thanks I will start with setting up the cuda environment and some basic gpu codes first.


Online
 Profile  
 
Display posts from previous:  Sort by  
 Page 4 of 5 [ 50 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: