-
Binary traslator
I took that challenge on this site to change decimal to binary, it works ok exept the binary numbers come out in reverse. Any way to fix?(i stuck a \n in there to make it go up and down, makes it a little better)
Code:
#include <iostream>
using namespace std;
int main()
{
int deci = 1;
const int base2=2;
cout<< "Enter a number you want in binary: ";
for(;;)
{if(deci == 0)
{cout<< "Enter another number you want in binary: ";}
cin>> deci;
cin.ignore();
while (deci >= 0)
{
cout<< " " <<deci % base2 << "\n";
deci = deci / base2;
if (deci == 0)
break;
}
continue;
}
cin.get();
return 0; }
-
There are several ways you could do this. One is to keep a vector/list of digits found and print them out in reverse, which might be too advanced for you. Another is to move your code into a recursive function that prints out the digits only at the end of the function (meaning all later digits will have already been printed out). This also is difficult if you don't know functions or recursion.
Perhaps the way to do it with the least advanced features is to keep a decimal value of the current result. So each digit you create you add to the result. The trick is to multiply by a one more power of ten each time. Basically, start with multiplier = 1 (which is 10 to the 0 power). Then at the end of the loop multiply the multiplier by 10 so that it refers to the next higher digit. Instead of outputting the deci % base2 value, multiply it by the multiplier and add it to the result. When the loop is done the result will have the correct value in base2 and you can print it out.
Example: decimal = 13.
Result = 0
Multiplier = 1
Loop 1
Base2 digit = decimal % 2 = 13 % 2 = 1
decimal = decimal / 2 = 13 / 2 = 6
Result = Result + (Base2 digit * Multiplier) = 0 + (1 * 1) = 1
Multiplier = Multiplier * 10 = 1 * 10 = 10
Loop 2
Base2 digit = decimal % 2 = 6 % 2 = 0
decimal = decimal / 2 = 6 / 2 = 3
Result = Result + (Base2 digit * Multiplier) = 1 + (0 * 10) = 1
Multiplier = Multiplier * 10 = 10 * 10 = 100
Loop 3
Base2 digit = decimal % 2 = 3 % 2 = 1
decimal = decimal / 2 = 3 / 2 = 1
Result = Result + (Base2 digit * Multiplier) = 1 + (1 * 100) = 101
Multiplier = Multiplier * 10 = 100 * 10 = 1000
Loop 4
Base2 digit = decimal % 2 = 1 % 2 = 1
decimal = decimal / 2 = 1 / 2 = 0
Result = Result + (Base2 digit * Multiplier) = 101 + (1 * 1000) = 1101
Multiplier = Multiplier * 10 = 1000 * 10 = 10000
decimal <= 0 --> break.
Result is 1101. Output.
-
Well, you'll want to start at the high order bits instead of the low order bits. So, what you'll really need to do is use a mask to compare binary values, one bit at a time (I am assuming that you don't want to use std::bitset).
To shift your mask, you will want to use the bitshift operators >>, starting with only a 1 in the high order position, and then shift it over, and compare using &.
So, an example of one test.
unsigned char num = 14; // unsigned char used because it is small
unsigned char mask = 128;
print 1 iff (num & mask != 0)
print 0 otherwise
So, in the example,
num is 0000 1110 and mask is 1000 0000, so you print 0
Then, you shift mask over one space and repeat
And, the best way to get a 1 in the highest order position for an unsigned integral type would be:
Code:
#include <limits>
unsigned int mask = 1 << (std::numeric_limits<unsigned int>::digits - 1);