Decimal To Binary Algorithm

This is a discussion on Decimal To Binary Algorithm within the C++ Programming forums, part of the General Programming Boards category; I'll just jump right into it, this is from the challenges page on the site (not the code, but the ...

  1. #1
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11

    Decimal To Binary Algorithm

    I'll just jump right into it, this is from the challenges page on the site (not the code, but the challenge itself: Convert decimal number to binary)

    I was wondering if you could give me your thoughts on this solution. Now I know that the use of the stl stack isn't the best thing in this situtation, but it just helps to display the bits in the correct order. My problem is that the time complexity of this function is O(N), and I try to stay way from O(N) time when ever possible (although its a norm for alot of algo). I was wondering if anyone has a better solution to displaying the bits (not getting the bits, but just displaying them) in less then O(N)?

    Code:
    void g_DecToBinary(unsigned int dec_number) {
    	std::stack<unsigned int> binary_number;
    	unsigned int old_dec_number = dec_number,
    				 bit;
    
    	while(dec_number != 0) {
    		bit = dec_number % 2;
    
    		dec_number /= 2;
    
    		if(bit)
    			binary_number.push(1);
    		else
    			binary_number.push(0);
    	}
    
    	std::cout << "The decimal number " << old_dec_number << " converted to binary is: ";
    
    	while(!binary_number.empty()) {
    		std::cout << binary_number.top();
    
    		binary_number.pop();
    	}
    
    	std::cout << std::endl;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Easiest way to convert a num to binary is by using bitwise operator.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Specifically, >> (or >>= or <<) and & should work.

    Search the board, this has come up many times before.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    Thanks for the replys.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Interesting thread, I'd thought I post what came to mind here.

    Regards,
    Brian
    Code:
    #include <iostream>
    #include <bitset>
    
    using std::cin;
    using std::cout;
    using std::bitset;
    
    int main(int argc, char *argv[])
    {
    	if(argc == 2)
    	{
    	    int i = atoi(argv[1]);
    	    bitset<sizeof(i)*8> b = i;
    	    cout << b << "\n";
    	}
    	else
    	    cout << "Usage: progName intValue\n";
    	
    	cout << "Press 'Enter' to continue . . . ";
    	cin.sync();
    	cin.ignore();
    	return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Damn binary to decimal algorithm
    By xmltorrent in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2006, 04:30 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21