binary division

How do i perform binary division, i want both the remainder and result of the division.. I am kinda confused on how I should to it..
???

1
2
int result = num1 / num2;
int remainder = num1 % num2;


using binary numbers.. it seems the only way i can use binary number is by using bitset..
The only way?
All the built- in integer types are binary numbers.

I think you need to clarify what particular context or usage you mean.
Numbers are numbers.
Division is division.

"Binary", "Decimal", "Hexadecimal" are just different TEXTUAL ways to display numbers.

But how you display the number through text does not change the underlying operation. ten dividied by five is two... regardless of whether or not you display it as:

dec: 10 / 5 = 2
hex: $A / $5 = $2
bin: %1010 / %0101 = %0010



So when you say you have "binary numbers"... what does that mean? Do you have a string or something? If so... convert the string to an integer... then do the division normally.
So this simple code should do the trick??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include <iostream>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	double dividend;
	double divisor = 1011;
	double result;
	double rest;
	cout << " dividend ?" << endl;
	cin >> dividend;
	result = dividend/divisor;
	rest = dividend/divisor;
	cout <<"for dividend " << dividend << " result:  " << result << " rest: " << rest << endl;
	system("pause");
 	return 0;
}


but the output is
 for dividend 1001  result: 0.990109   rest: 0.990109 


using http://www.csgnetwork.com/binmultdivcalc.html

it say
result = 0
rest = 101
Last edited on
Well if you want the remainder, you'd want to use integers rather than doubles.

Also... 1011 is one-thousand eleven... is that the number you intended? Or did you want eleven (which would be "1011" in binary)?

Also whatever number you get from the user is going to be in decimal, not in binary... because the user is inputting text and not actually a number. cin will assume the text is in decimal unless you tell it otherwise.

Unfortunately I don't think there's a way to tell cin to parse the text as binary, so you'll have to do that on your own.

And if you want to print the result as binary... you'll have to write your own function to format the number into a binary string. Or maybe you can use bitset to do this... honestly I'm not at all familiar with bitset because I never use it. So I just wrote my own functions to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int getIntFromBinaryText(const char* text)
{
    int value = 0;
    while(*text)
    {
        value <<= 1;
             if(*text == '1')    value |= 1;
        else if(*text == '0')    ;          // do nothing
        else                     return -1; // invalid input... return a negative number to indicate error
        ++text;
    }

    return value;
}

std::string getBinaryTextFromInt(int value)
{
    std::string text;
    // note I'm assuming 'value' is positive here
    
    while(value > 0)
    {
        if(value & 1)       text += '1';
        else                text += '0';
        value >>= 1;
    }
    
    if(text.empty())        return "0";
    std::reverse( text.begin(), text.end() );       // #include <algorithm>
    return text;
}

int main()  // note:  main... not that _tmain garbage.
{
    int divisor = getIntFromBinaryText("1011");  // or just "divisor = 11;"
    
    std::string text;
    cout << " dividend ?" << endl;
    cin >> text;
 
    int dividend = getIntFromBinaryText(text.c_str());
    if(dividend < 0)
    {
        cout << "Invalid input";
        return 1;
    }
    
    int result = dividend / divisor;
    int remainder = dividend % divisor;
    
    cout << "dividend=" << getBinaryTextFromInt(dividend);
    cout << "    divisor=" <<  getBinaryTextFromInt(divisor);
    cout << "    result=" <<  getBinaryTextFromInt(result);
    cout << "    remainder=" <<  getBinaryTextFromInt(remainder);
    
}


Results:

http://ideone.com/U3Ul0O
Topic archived. No new replies allowed.