C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 12th, 2007, 10:31 PM
#<Hspd7> #<Hspd7> is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 12 #<Hspd7> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 13 m 2 sec
Reputation Power: 0
Help with Unsigned integers

Ok so the following code contains unsigned integer seed... which is not supposed to be able to store negative integers... correct?

Why then, when I run the program does it give a set of random numbers after I enter a negative number, this is evading me for some odd reason, any explanation would be appreciated, thanks in advance guys.



Code:
1   // Fig. 3.9: fig03_09.cpp
2   // Randomizing die-rolling program
3   #include <iostream.h>
4   #include <iomanip.h>
5   #include <stdlib.h>
6   
7   int main()
8   {
9      unsigned seed;
10  
11     cout << "Enter seed: ";
12     cin >> seed;
13     srand( seed );
14  
15     for ( int i = 1; i <= 10; i++ ) {
16        cout << setw( 10 ) << 1 + rand() % 6;
17  
18        if ( i % 5 == 0 ) 
19           cout << endl;
20     }
21  
22     return 0;
23  

Reply With Quote
  #2  
Old January 12th, 2007, 10:41 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 14
Send a message via AIM to ubergeek
You can assign a negative number to an unsigned integer, and it will be represented as if it had been declared signed. However, it wasn't declared signed so the compiler generates no code to handle that. Because of how negatives are repesented at the bit level, seed will end up being a ridiculously large number. Note that in gcc if you try and assign a negative number to an unsigned integer in the code, it will give a warning.

Reply With Quote
  #3  
Old January 12th, 2007, 10:54 PM
#<Hspd7> #<Hspd7> is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 12 #<Hspd7> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 13 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by ubergeek
You can assign a negative number to an unsigned integer, and it will be represented as if it had been declared signed. However, it wasn't declared signed so the compiler generates no code to handle that. Because of how negatives are repesented at the bit level, seed will end up being a ridiculously large number. Note that in gcc if you try and assign a negative number to an unsigned integer in the code, it will give a warning.


Ok I think I understand. thanks.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with Unsigned integers


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap