Results 1 to 3 of 3
  1. #1
    1337/PWNR
    Programmer
    Emu's Avatar
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
         User Info     Contact     Gamer ID
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
    Gamer IDs

    Gamertag: SpidermaN EaT PSN ID: Shaq23Jordan

    Contact info:

    Youtube Channel: www.youtube.com/eemuohg Kik Username: ShaqGlizzy

    Default binary conversions to octal example

    Hello again CL, today I have finished my binary conversion part of my program and have tackled all three conversions in one function. This functions speed efficiency is bigO(0), which again, is the fastest runtime. The reason I wanted to post this is because all over the net I see people take long ass ways just to convert a binary number to a decimal and I am like, "Wtf? Why are they making it harder than it has to be?".

    All I need to do with this code now is make an error check for the input not being binary.
    Code:
    //fnc to convert binary to all conversions, octal being the highest
    void binaryToDecimal(int conversion) {
    	int dec=0; //dec result
    	int binary; //user input binary
    	int bits=64; //64bit binary
    
    	system("cls");
    	cout << "Input binary digit: ";
    	cin >> binary;
    
    	cout << endl;
    
    	//loop for binary math conversion
    	for (int i=0; i<bits; i++) {
    		//if remainder equals 1 then add 1(2^i)
    		if (binary%10==1)
    			dec=dec+1*(int)pow(2, i);
    
    		//cut 10 off from the binary
    		binary=binary/10;
    	}
    	
    	if (conversion==0) //decimal
    	cout << "resultant: " << dec;
    	else if (conversion==1) //hex
    		cout << "resultant: " << "0x" << hex << dec;
    	else //octal
    		cout << "resultant: " << oct << dec;
    
    	cout << endl;
    	cout << endl;
    
    	system("pause");
    	system("cls");
    }
    Behind the thinking:

    In binary there are only two numbers who are 1 and 0. So this also would mean there are only two remainders when divided by 10, who are also 1 and 0. for every 1 we come across we add 1(2^i), since binary is based off of powers of two. For every 0 we come across we add 0(2^i) and since anything times 0 = zero(nothing) we would technically do nothing for the zeros we come across, basically skipping them.

    1 Not allowed! Not allowed!
    Last edited by Emu; 10-15-2013 at 06:33 AM. Reason: updating info

  2. #2
    Desu Development
    Pomf pomf :3
    Fuck Sky's Avatar
    Join Date
    Sep 2013
    Location
    WOW ANIME MAN WOW
    Posts
    1,137
    Points
    1,687
         User Info     Contact     Gamer ID
    Join Date
    Sep 2013
    Location
    WOW ANIME MAN WOW
    Posts
    1,137
    Points
    1,687
    Gamer IDs

    Gamertag: Hexpresso PSN ID: WideDongWeary Steam ID: twitchhexpresso

    Contact info:

    Youtube Channel: http://www.youtube.com/ Kik Username: Wownicehack

    Default

    Sad to see someone who knows what they're doing get no response.

    1 Not allowed! Not allowed!
    geadszf1231qewrg

  3. #3
    1337/PWNR
    Programmer
    Emu's Avatar
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
         User Info     Contact     Gamer ID
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
    Gamer IDs

    Gamertag: SpidermaN EaT PSN ID: Shaq23Jordan

    Contact info:

    Youtube Channel: www.youtube.com/eemuohg Kik Username: ShaqGlizzy

    Default

    Quote Originally Posted by Yuri View Post
    Sad to see someone who knows what they're doing get no response.
    Thanks + BUMP

    0 Not allowed! Not allowed!

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -10. The time now is 07:49 AM.
Powered by vBulletin®
Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.