Skip to content

Commit

Permalink
2scomp
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfm1211 committed May 11, 2024
1 parent 3b15979 commit c34a5cb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
66 changes: 66 additions & 0 deletions 2s-complement/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>8-bit Two's Complement Calculator</title>
<style>
* {
font-size: 24pt;
font-family: sans-serif;
}
#binout, #decout {
font-family: monospace;
}
</style>
</head>
<body>
<h1>8-bit Two's Complement Calculator</h1>
<label for="decin">Decimal to 2s Complement:</label>
<input type="text" id="decin">
<br>
<div id="binout"></div>
<br>
<br>
<label for="binin">2s Complement to Decimal:</label>
<input type="text" id="binin">
<br>
<div id="decout"></div>
<script>
const $ = q => document.querySelector(q);

$("#decin").addEventListener("keyup", () => {
const num = parseInt($("#decin").value);
if (isNaN(num) || num > 127 || num < -128) {
$("#binout").textContent = "Invalid input.";
return;
}
if (num < 0) {
const bin = [...Math.abs(num + 1).toString(2).padStart(8, "0")];
bin[0] = "1";
for (let i = 1; i < 8; i++)
bin[i] = bin[i] == "0" ? "1" : "0";
$("#binout").textContent = bin.join("");
} else {
$("#binout").textContent = num.toString(2).padStart(8, "0");
}
});

$("#binin").addEventListener("keyup", () => {
const twos = $("#binin").value;
if (twos.length != 8 || [...twos].some(c => c != "0" && c != "1")) {
$("#decout").textContent = "Invalid input.";
return;
}
if (twos[0] == "0") {
$("#decout").textContent = parseInt(twos.substring(1), 2);
} else if (twos[0] == "1") {
const bin = [...twos];
for (let i = 0; i < 8; i++)
bin[i] = bin[i] == "0" ? "1" : "0";
const num = -parseInt(bin.join(""), "2") - 1;
$("#decout").textContent = num.toString();
}
});
</script>
</body>
</html>
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<li><a href="para">Parallelogram Point Finder</a></li>
<li><a href="https://github.com/michaelfm1211/ping-calculator">Ping Packet Calulcator</a></li>
<li><a href="harmonics">Square Wave Harmonics Calculator</a></li>
<li><a href="2s-complement">8-bit Two's Complement Calculator</a></li>
</ul>
</body>
</html>

0 comments on commit c34a5cb

Please sign in to comment.