PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 9, 2022

[FIXED] How to convert 10^4 to binary

 August 09, 2022     binary, computer-science, decimal, math     No comments   

Issue

I need to convert 10^4 to binary

expanding it will give me a large number and dividing that by 2 a bunch of times will be really inefficient

10^4 = 10000

how do i do it directly


Solution

I would write a recursive function, the pseudo-code is here:

int Convert_to_binary (x):
     if(x == 0):
        return 1;
     if(x == 1):
        return 10;
     if(x%2 == 1):
        return Convert_to_binary(x-1)+1;
     if(x%2 == 0):
        return Convert_to_binary(x/2)*10;

This will return the binary format as an integer like 2 is 10 in binary and 1 is 1 in binary format and 3 is 11 and so on



Answered By - Mahta Shafieesabet
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing