binary number input
May 31, 2013 at 12:11pm UTC
I am new to C++ language, could any one help me to figure out " how to take binary number as an input, generate partial products by bit-wise multiplication and in last step to add all the partial products to generate final products".
May 31, 2013 at 12:12pm UTC
Homework question?
May 31, 2013 at 12:17pm UTC
what do you mean by homework question?
May 31, 2013 at 12:24pm UTC
It sounds like a homework question, to which I won't just give you the code. However, a google search of bitwise operators in C++ should help.
1 2 3
int num = 4;
num = num<<1;
std::cout << "Num = " << num;
That should get you started.
Cheers,
Scott
Last edited on May 31, 2013 at 12:28pm UTC
May 31, 2013 at 1:53pm UTC
it is not homework question, i need it to implement Montgomery multiplication algorithm, if i declare int num; can i store binary value in it or i have to declare string for binary numbers???
Jun 1, 2013 at 3:26am UTC
Here you go. It validates input and formats the output. It's probably not the most efficient but it does what it needs to.
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
void clearScreen();
std::string validateEntry();
std::string decimalToBin(int number);
int binaryToDec(std::string number);
int main()
{
clearScreen();
std::cout << "Enter First Binary Number: " ;
std::string binaryString1 = validateEntry();
int binaryNum1 = binaryToDec(binaryString1);
std::cout << "Enter Second Binary Number: " ;
std::string binaryString2 = validateEntry();
int binaryNum2 = binaryToDec(binaryString2);
std::vector<int > partials(binaryString2.length(), 0);
int bitShift = 0;
int total = 0;
for (int i = (binaryString2.length() - 1); i >= 0; i--)
{
if (binaryString2.compare(i, 1, "1" ) == 0)
{
partials[bitShift] = binaryNum1<<bitShift;
}
else
partials[bitShift] = 0;
total += partials[bitShift];
bitShift++;
}
std::string totalStr = decimalToBin(total);
total = totalStr.length();
clearScreen();
std::cout << std::fixed
<< std::setw(total) << binaryString1 << "\n"
<< std::setw(total) << binaryString2 << "\n" ;
for (int i = 0; i < total; i++)
std::cout << "-" ;
std::cout << "\n" ;
int zeroBuf = binaryString1.length() - 1;
for (int i = 0; i < (binaryString2.length()); i++)
{
std::string tempStr = decimalToBin(partials[i]);
if (tempStr.length() == 1)
{
for (int j = 0; j < zeroBuf; j++)
tempStr += "0" ;
}
zeroBuf++;
std::cout << std::fixed << std::setw(total)
<< tempStr << "\n" ;
}
for (int i = 0; i < total; i++)
std::cout << "=" ;
std::cout << std::fixed << "\n"
<< std::setw(total) << totalStr << "\n\n" ;
}
void clearScreen()
{
std::cout << "\033[2J\033[1;1H" ;
}
std::string validateEntry()
{
bool valid = true ;
std::string tempStr;
do
{
valid = true ;
getline(std::cin, tempStr);
for (int i = 0; i < tempStr.length(); i++)
{
if ((tempStr.compare(i, 1, "0" ) != 0) && (tempStr.compare(i, 1, "1" ) != 0))
{
valid = false ;
clearScreen();
std::cout << "Enter Valid Binary Number: " ;
break ;
}
}
} while (valid == false );
clearScreen();
return tempStr;
}
std::string decimalToBin(int number)
{
if ( number == 0 ) return "0" ;
if ( number == 1 ) return "1" ;
if ( number % 2 == 0 )
return decimalToBin(number / 2) + "0" ;
else
return decimalToBin(number / 2) + "1" ;
}
int binaryToDec(std::string number)
{
int result = 0, pow = 1;
for ( int i = number.length() - 1; i >= 0; --i, pow <<= 1 )
result += (number[i] - '0' ) * pow;
return result;
}
Last edited on Jun 1, 2013 at 3:26am UTC
Topic archived. No new replies allowed.