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
Is there a computer expression?
Before that lets see some interesting patterns of binary ,see Fig.2
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