Skip to content

Commit

Permalink
feature(b2d-conversion-viceversa) : Binary to decimal conversion and …
Browse files Browse the repository at this point in the history
…viceversa
  • Loading branch information
srmarohit committed May 11, 2024
1 parent 6fab478 commit 7c4d327
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions rohit/js/b2d-conversion-viceversa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function B2D(binNumber) {
let bn = binNumber;
let res = 0;
let itr = 0;

while (bn > 0) {
let last_digit = bn % 10;
bn = parseInt(bn / 10);

if (last_digit !== 0 && last_digit !== 1) {
return "Invalid Binary Number";
}

res += last_digit * 2 ** itr;

itr++;
}

return res;
}

function D2B(decNumber) {
// 9
let dn = decNumber;
let res = 0;
let fact = 1;

while (dn > 0) {
rem = dn % 2;
res += rem * itr; //1
dn = parseInt(dn / 2); ///4
fact *= 10;
}

return res;
}

console.log(B2D(1001));
console.log(D2B(26));

0 comments on commit 7c4d327

Please sign in to comment.