CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2007
    Posts
    3

    binary calculation overflow?

    I had to write a binary to decimal conversion program for a exercise in a c++ book.

    My program works fine, but it seems to be running out of memory space for the variable. If i use "int" to declare my variables the program will work fine up to an output of 1024. I tried even declaring as __int64 and the code will output correctly up to 524XXX (19bit).

    edit: guess i should make it clear how entering a 16bit binary for instance will not output correctly as 65535 when variables are declared "int". It output instead to some garbage of 186 or something. I have to declare variables to __int32 to output 16bit and _int64 outputs up to around 19bit

    this doesnt seem right..

    does my code seem ok?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	__int64 binary;
    	__int64 total = 0;
    	
    	cout << "Enter binary number: ";
    	cin >> binary;
    
    	for ( int count = 1;binary != 0;count *= 2)
    	{
    		if ( binary == 1 )
    		{
    			total += count;
    			binary = 0;
    			break;
    		}
    		if ( binary % 2 )
    			total += count;
    		binary /= 10;
    	}
    		
    
    	cout << "\nDecimal equivilant is: " << total << endl;
    	
    	cout << "\n\n";
    	system("PAUSE");
    
    	return 0;
    }
    Last edited by #Christopher#; October 7th, 2007 at 08:54 PM.

  2. #2
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: binary calculation overflow?

    The exercise exists because they want you to read up a binary (string) and convert it into a Decimal (number).

    e.g. read this 011 as string, interpret it as binary, and transform it into decimal 3.

    What you have got yourself is - if someone inputs 011 your program interprets it as decimal 11. If you think about it you will realize why your program fails after 1024.

    The internal representation of a number in computer memory is anyways binary; if you can directly read a binary number you do not need much of code to print out the number in decimal format --> Just printing out the number would do the trick.

    So try this - Read the binary number as string and then write a program to convert it into decimal.

    PS - there may be a switch or something that may directly allow you to interpret the input as a binary number. Plain and simple cin >> would just interpret it as radix 10 number - you need to look up a format specifier that will interpret it as base 2 number. Then you will have a 2 line program -> Read input in correct format and output it in correct format (which is simple cout of the number).
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  3. #3
    Join Date
    Oct 2007
    Posts
    3

    Re: binary calculation overflow?

    This code doesnt output 011 as 11, it outputs as 3 correctly....

    Every binary number I have enter has computed correctly.

    This is supposed to be a simple chapter 2 program, and the lesson is to use what you have learned so far in the book, not some special switch. Im sure there is a better way to write it.

    but no worries, it was just annoying me that it works fine up to <1024 with int or long, and then will compute correct output to around <525000 with __int64.

    using VS2008 beta, and 32bit vista

    edit: like i said it supposed to be simple program with chapter 2 type knowledge, maybe it is not supposed to work efficiently.
    Last edited by #Christopher#; October 8th, 2007 at 05:54 AM.

  4. #4
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: binary calculation overflow?

    1. You are going in a very roundabout way of doing things.
    2. Your code works because you have ** force ** treated your integer variable as a literal binary representation of your input (which it is not) and worked an algorithm around it.
    3. As I said that if you input 011 to your program, the number that goes into memory is decimal 11 (NOT decimal 3, or binary 011). If you feed 0111 to your program, the number that goes into memory in that integer variable is decimal 111 (and NOT decimal 7 or binary 0111). If you doubt this put a watch onto your variables and inspect in hex/binary mode.
    4. The problem is definitely a chapter 2 problem. I gave you a hint about treating the input as a string. You can then loop over the digits of your input using about the same logic that you have used (multiplying the power by 2 on each iteration of the loop).
    5. Another hint was that the way you have treated the input, the things are going to fall after 1024, because anything beyond 1024 is 11 digits long (in binary). The max an (unsigned) integer can represent is the decimal number 4294967296. If you try to stuff binary digits the way you have done, then consider this - the minimum 11 digit number 10000000000 is greater than 4294967296 - it would take more than 32 bit digits to store that number in memory.
    6. Please try to have another go at your program with these suggestions. (Or wait and someone will write the program for you and spoil all your learning )
    Last edited by UnderDog; October 8th, 2007 at 06:19 AM.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  5. #5
    Join Date
    May 2002
    Posts
    1,435

    Re: binary calculation overflow?

    UnderDog gave good advice to use a string for the input. You won't run into the same limitations as when using an integer to hold a decimal number that only looks binary.

    With your method, when you enter a decimal number like 1100100 and consider it to be binary the 'storage space' required is over one million (1,100,100) while its decimal value according to your code design is actually 100. It should be obvious that you will run out of storage space quickly by considering a decimal number to be binary.

  6. #6
    Join Date
    Oct 2007
    Posts
    3

    Re: binary calculation overflow?

    yea, i understand

    writing all these small programs yesterday is starting to make me miss the obvious.

    the program is correctly written for the chapter 2 exercise.

    but yes, i was thinking of my input as binary instead of a integer.

    thanks for the response,

    now i feel like a idiot =p

    laterz

  7. #7
    Join Date
    May 2007
    Posts
    5

    Re: binary calculation overflow?

    u r reading the input in a wrong way. u are storing the binary number in integer form. therby causing an overflow of the input variable.
    for example bin 1111 1111 1111 which is jus int 4096(well within the range of ur 'total' variable) is being stored as int 111111111111, which is a huge number and falls out of range of the'binary' variable.
    Secondly, you have no kind of check system, ur program doesnt realize if the input number is binary type or not, if individual digits are either 0 or 1 so when an input varaible overflow occurs and u have sum negative int in the varaible, ur algorithm produces an incorrect answer.
    Putting a check might be difficult for u at this stage, but the whole point of this exercise is that u understand the limitations of data types, binary jus cant be read as an int. in small values u would store 1 byte 0011 as jus a 2 bit binary(which doesnt really do ne harm) n u run out memory very soon in case of large numbers.
    read the input as a string and iterate thru the 'bits' to evaluate the value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured