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 26th, 2006, 07:56 AM
ravisinghhoney ravisinghhoney is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 1 ravisinghhoney User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 49 sec
Reputation Power: 0
Red face Calculating the maximum value of integer

This seems to be a simple question but i would like to know ideas to make a program to calculate the largest value of integer.There is the in built function max_int,but i would like to calculate this value without using max_int.

One way is just add on 1 to integer till we get a negative value but it is time consuming,i would like some ideas to do it more easily..
I hope the great minds would help

Reply With Quote
  #2  
Old January 26th, 2006, 10:53 AM
Icon's Avatar
Icon Icon is online now
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 756 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 35 sec
Reputation Power: 4
I am not sure why you want this but a possible way could be:
c Code:
Original - c Code
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. int main()
  5. {
  6.   int i,j;
  7.  
  8.   i = j = 0;
  9.  
  10.   for( ; j<(sizeof(int)*8)-1; j++ )
  11.   {
  12.     i <<= 1;
  13.     i |= 1;
  14.   }
  15.  
  16.   printf("%d\n%d\n",i,INT_MAX);
  17.  
  18.   return 0;
  19. }


But this only works for 2's complement (and 1's complement), for details about that I have to refer you to Itsacon, he's the bit king in my opinion ;-)

Reply With Quote
  #3  
Old January 26th, 2006, 02:47 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Indeed we can do better

Note that the following only works on 2's complement systems, but you'll be hard pressed to find anything that isn't these days :-)

c Code:
Original - c Code
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. int main()
  5. {
  6.     /*  Declarations  */
  7.     int i;
  8.     unsigned int j;
  9.  
  10.     /*  Do magic  */
  11.     i = -1;   /*   First we fill i with all 1's   */
  12.     j = 0 | i;    /*    we transfer this to j. Don't use j = i, since this might **** up the sign bit on some systems.  */
  13.     j >>= 1;    /*  we make the highest bit zero. Has to be done on unsigned, otherwise a 1 gets shifted in   */
  14.  
  15.     /*  Output result */
  16.     printf("%u\n%d\n",j ,INT_MAX);
  17.  
  18.     /*  Return succes */
  19.     return 0;
  20. }


Note that line 12 COULD be replaced with
Code:
j = (unsigned int) i;
But the behaviour for isn't documented, it might screw up the sign bit. By doing it this way, you're talking bits only and the system will surely not **** anything up.

Note that all can be done even shorter, setting j to zero and substracting 1 for example, but it's less neat (overflows and such) and if you wanted simple, you could just use MAX_INT :-)
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Reply With Quote
  #4  
Old January 26th, 2006, 02:56 PM
Icon's Avatar
Icon Icon is online now
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 756 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 35 sec
Reputation Power: 4
Hmm it seems the shift operator is already zero-filled.. So this also seems to work (at least on my box):
C Code:
Original - C Code
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. int main()
  5. {
  6.         /*      Declarations    */
  7.         unsigned int j;
  8.  
  9.         /*      Do magic        */
  10.         j = -1;
  11.         j >>= 1;
  12.  
  13.         /*      Output result   */
  14.         printf("%u\n%d\n",j ,INT_MAX);
  15.  
  16.         /*      Return succes   */
  17.         return 0;
  18. }

Reply With Quote
  #5  
Old January 26th, 2006, 02:59 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Because you're using an unsigned int.
Like I said, assigning -1 to an unsigned int works, but is undefined behaviour, and MIGHT go wrong. If you want to do weird stuff, be safe, right?
Comments on this post
Icon agrees: You're still the reigning bit king here

Reply With Quote
  #6  
Old January 26th, 2006, 03:05 PM
Icon's Avatar
Icon Icon is online now
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 756 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 35 sec
Reputation Power: 4
This is safe I think, it is the opposite of your example:
C Code:
Original - C Code
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. int main()
  5. {
  6.   /*  Declarations  */
  7.   int j;
  8.  
  9.   /*  Do magic  */
  10.   j = 1;
  11.   j <<= (sizeof(int)*8-1);
  12.   j = ~j;
  13.  
  14.   /*  Output result */
  15.   printf("%u\n%d\n",j ,INT_MAX);
  16.  
  17.   /*  Return succes */
  18.   return 0;
  19. }
Comments on this post
Itsacon agrees: Well done, young padawan! :-D

Reply With Quote
  #7  
Old January 26th, 2006, 05:29 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2Folding Points: 911244 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Yeah, works too, though IMHO, using sizeof is the same as using MAX_INT
otherwise you could just do pow(2, (sizeof(int) * 8)) - 1 (which is the same as pow(256, sizeof(int)) - 1....

Getting bored yet?

Reply With Quote
  #8  
Old January 27th, 2006, 01:30 PM
Icon's Avatar
Icon Icon is online now
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 756 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 35 sec
Reputation Power: 4
Offcourse... Simple rewrites don't count..

Besides that, pow is a _much_ more expensive operation and you'll have to include math..

Reply With Quote
  #9  
Old February 15th, 2006, 01:07 PM
UnderWaterman UnderWaterman is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 UnderWaterman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 30 sec
Reputation Power: 0
0111 1111
:-d

i would just subtract one from unsigned int

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Calculating the maximum value of integer


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