Skip to main content

Decimal to Binary, New equation!

 Have you ever imagined we could convert decimal to binary using an expression?

Well no, we always use a `long division` like method ,see Fig.1

Fig.1: image source

Is there a computer expression?

Before that lets see some interesting patterns of binary ,see Fig.2
Fig.2 notice the image carefully

As you can see 
in row 1 patern goes like 0,1,0,1,0,1,0,1 because row 1 is for [place holder 1-top to bottom]



in row 3 patern goes like 0,0,0,0,1,1,1,1 because row 3 is for [place holder 4-top to bottom]
Note: I mean rows as from right to left

The expression!

Values Here:
Number: 0-infinity (a value to be converted to binary)
binary holder: 1 / 2 / 4 / 8 / 16 / 32 / ... (Place of binary needed, just like tens, hundreds)
Result: 0 or 1
 
Is it not interesting when there is no if condition, and we can get 0 or 1 with just a simple expression?

Expression in words:
expression 1: [the number, subtracted from (reminder we get ,when we divide the same number by binary holder)]

note that, result of expression 1 is completely divisible by binary holder.
expression 2: [expression 1 divided by binary holder and getting its reminder when we divide it by 2]
the answer of expression 2 is either 0 or 1.

Expression in code(js):

function placeNeeded(number) {
  var i;
  for (i = 1; Math.pow(2, i) <= number; i++) {}
  return i;
}

function FindBinary(Number) {/*main Function*/
  var x;
  var placeRequired = placeNeeded(Number); /*instead of the function you can even set 5 or 6,
but if the Number(like 19) exceeds,then some of binary digits will be trimmed,BE AWARE,using the function is better*/
  var BinaryValue = "";
  var binaryHolder = 1;
  for (x = 1; x <= placeRequired; x++) {
    var Algorithm =
      ((Number - (Number % binaryHolder)) / binaryHolder) %
      2; /*main Algorithm*/
    BinaryValue = Algorithm + BinaryValue;

    console.log(BinaryValue, "Debug output(from function)");//REMOVE THIS LINE
    binaryHolder += binaryHolder;
  }
  return BinaryValue;
}
console.log(FindBinary(19), "Result");//Use it AS PER YOUR NEED

Popular posts from this blog

Scratch 3.0 for windows7 !

Have You Ever Imagined Scratch 3.0 will be possible  for  windows 7 ?  Well, yes, it's possible and very easy  Simple Method! Prerequisite: Adobe AIR is not required but installing it will solve some possible errors, so install it from here:  here   Visit  here  to download your scratch 3.0 for windows 7! Next, continue all the installation Prompts, and Hoo-Hoo! Your scratch 3.0 starts running! The Developers Method! Do you feel downloading from an unofficial site is wrong? Well then you can  make your own   Official Version Since you're a Developer, you should already have: git-scm node js and npm A text editor(not required now) If not Make sure you install it. Run the following commands in the command prompt, one by one git clone https://github.com/LLK/scratch-desktop.git cd scratch-desktop npm install Now that we have done the setup, it's time to make our exe! and the trick is running the build command given below make the exe c...