Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(b2d-conversion-viceversa) : Binary to decimal conversion and … #28

Merged
merged 1 commit into from
May 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Loading