Divide by two to convert decimal to binary

Simple C++ program that converts from decimal to binary by dividing a given number by two repeatedly, saving the remainder to a boolean vector, and then printing the contents of the vector back.
capture

#include <iostream>
#include <vector>
////////////////////////////////////////////////////////////////////////
void printBinary(std::vector passedBool);                       //
std::vector divisionAlgorithm(unsigned long long userInput);    //
////////////////////////////////////////////////////////////////////////
int main(void)
{
	unsigned long long foo = 0;
	for(int i = 0; i < 5; i++)
	{
		std::cout << "Please enter a number: "; std::cin >> foo;
		std::cout << std::endl;
		// divisionAlgorithm returns a boolean vector, and printbinary
		// prints the returned vector
		printBinary(divisionAlgorithm(foo));
	}
	return 0;
}
////////////////////////////////////////////////////////////////////////
void printBinary(std::vector passedBool)
{
	// Reverse iterator... faster than inserting to front
	for(auto it = passedBool.rbegin(); it != passedBool.rend(); ++it)
	{
		std::cout << (*(it));
	}

	std::cout << std::endl;
	return;
}
////////////////////////////////////////////////////////////////////////
std::vector divisionAlgorithm(unsigned long long userInput)
{
	std::vector returnBool;
	// while the passed input is non 0
	while(userInput)
	{
		returnBool.push_back(userInput % 2);
		userInput /= 2;
	}
	// Return the vector
	return returnBool;
}
////////////////////////////////////////////////////////////////////////

Leave a Reply

Your email address will not be published. Required fields are marked *