C ProgrammingDiscussion
Decimal to Binary Converter

StumblephonicFeb 23, 3:49pm
Hi, I am working out a lil' program to convert decimal numbers to binary numbers. I know these progs are a dime-a-dozen. How would you have done this? I am thinking of working out a way to use recursion. I am aware that I should make the prog handle negative numbers and numbers bigger than 65,535. This isn't for school. I just want to familiarize myself with binary numbers. This is what I have so far:

#include <iostream>
#include <vector>

int main(){

int number = 0 , result = 0 , bits = 0;
std::vector<int> binVct;

std::cout << "Enter a number: ";
std::cin >> number;

if( number <= 256 && number >= 0 ) bits = 8;
else if( number <= 65535 && number > 256 ) bits = 16;

for( int i = 0 ; i < bits ; i++ ){ /// dec2bin converter loop

result = number / 2;

if( number % 2 == 1 ) binVct.push_back( 1 );

else binVct.push_back( 0 );

number = result;

}

for( int i = binVct.size() - 1 ; i < binVct.size() ; i-- ){ /// print results loop

std::cout << binVct.at( i );

if( i == 8 ) std::cout << "-";

}

std::cout << "\n";

system( "PAUSE" );
return EXIT_SUCCESS;

}


Sponsor
KibiyamaFeb 23, 4:44pm
I would suggest using bitwise operators.

OR |
AND &
XOR ^
RIGHT-SHIFT >>
LEFT-SHIFT <<

Consider that, using shifts, you can essentially examine each bit individually.

StumblephonicFeb 23, 7:59pm
And I can quickly divide by 2 right?

EDIT** Ok, I right-shifted by one but I can't tell if it is any faster. Also, I don't see where I can use bitwise operators anywhere else. I'm thinking in this statement there might be a place:

if( number % 2 == 1 ) binVct.push_back( 1 );


Sponsor
KibiyamaFeb 23, 9:11pm
#include <iostream>
#include <sstream>

int main()
{
int number = 0;
std::stringstream str;
std::cin >> number;
for (int i = sizeof(int) * 8 - 1; i >= 0; i--)
{
str << (number >> i & 1);
}
std::cout << str.str() << std::endl;
return 0;
}

...of course there's also the really, really easy solution that I didn't tell you about...

There's a function called itoa that converts a number to a string in a base you pass to it. So itoa(number, str, 2) should give you the correct answer.

StumblephonicFeb 23, 9:39pm
um, k... so:

(number >> i & 1) means number shifted to the right i & 1 number of times? So sizeof(int)*8-1 = 31 and number is 43: (43 >> 31 & 1)?

43 >> 31&1 in binary is this: 00011111&00000001 = 00011111

so 43 right-shifted 00101011 >> 00011111 = 00000001?

DOn't worry about me dude, I'll get it soon enough! lol


Sponsor
KibiyamaFeb 24, 5:11am
Okay, here's what's going on.

43 in binary is 0010 1011.

When we right-shift it i places (for the sake of simplicity, let's say i is 1 right now) we get 0001 0101.

Then we AND it with 1, which is 0000 0001.

0001 0101
0000 0001
---------------
0000 0001

What's going on with AND is that, for each bit, it only sets it to 1 if both corresponding bits are 1 also.

1111 1111
0101 0101
---------------
0101 0101

1001 1001
0000 0000
---------------
0000 0000

1010 1010
0101 0101
---------------
0000 0000

So, if we AND any number with 1, it will always clear every bit except the last one, which will be set to 1 if the number ends in a 1, or 0 if it doesn't. So, in essence, you're looking at the last bit of the number.

And now here's the part where I tell you that this is all completely pointless. Modern computers are so fast at every operation that shifting and ANDing is mostly unnecessary, especially given that compiler optimizations will almost certainly generate the fast answer regardless of how you write it.

So the emphasis should really be on clarity. Just write it however you understand it best and let the compiler worry about making it fast. But it is a good thing to learn, even if you never use it.

StumblephonicFeb 24, 8:07pm
I do enjoy learning! You taught me something there. You learned it have you not? Thanks, Kibiyama!


Decimal to Binary Converter

You need to Sign-up for StumbleUpon to post to this forum