Skip to content

Commit b547c24

Browse files
committed
Initial commit
0 parents  commit b547c24

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# binvis
2+
A simple tool to visualize the binary system.

index.html

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css" />
7+
<link rel="stylesheet" href="/main.css" />
8+
<script src="/main.js" defer></script>
9+
<title>binvis</title>
10+
</head>
11+
<body>
12+
<header class="container">
13+
<hgroup>
14+
<h1>binvis</h1>
15+
<h2>Visualize the binary number system.</h2>
16+
</hgroup>
17+
</header>
18+
<hr>
19+
<main class="container">
20+
<article class="text-center">
21+
<p style="font-size: 70px;" id="binaryCounter">00000000</p>
22+
<p style="font-size: 40px;" class="text-secondary" id="base10Counter">0</p>
23+
<p class="text-secondary" style="margin-top: 0; font-size: 30px;">
24+
<span style="color: red" onclick="flipBit(128)" id="128bit">128</span>
25+
+
26+
<span style="color: red" onclick="flipBit(64)" id="64bit">64</span>
27+
+
28+
<span style="color: red" onclick="flipBit(32)" id="32bit">32</span>
29+
+
30+
<span style="color: red" onclick="flipBit(16)" id="16bit">16</span>
31+
+
32+
<span style="color: red" onclick="flipBit(8)" id="8bit">8</span>
33+
+
34+
<span style="color: red" onclick="flipBit(4)" id="4bit">4</span>
35+
+
36+
<span style="color: red" onclick="flipBit(2)" id="2bit">2</span>
37+
+
38+
<span style="color: red" onclick="flipBit(1)" id="1bit">1</span>
39+
</p>
40+
<small class="text-secondary"><i>Click each red or green number to flip its bit!</i></small>
41+
</article>
42+
</main>
43+
</body>
44+
</html>

main.css

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
:root {
2+
--pico-font-family: ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono', 'Roboto Mono', 'Oxygen Mono',
3+
'Ubuntu Mono', 'Source Code Pro', 'Fira Mono', 'Droid Sans Mono', 'Consolas', 'Courier New',
4+
monospace;
5+
}
6+
7+
.text-center {
8+
text-align: center;
9+
}
10+
11+
.text-secondary {
12+
color: gray;
13+
}
14+
15+
span {
16+
cursor: pointer;
17+
}

main.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const counter = document.getElementById("binaryCounter");
2+
const base10Counter = document.getElementById("base10Counter");
3+
const bits = {
4+
1: false,
5+
2: false,
6+
4: false,
7+
8: false,
8+
16: false,
9+
32: false,
10+
64: false,
11+
128: false
12+
}
13+
14+
function flipBit(bit) {
15+
const bitElement = document.getElementById(`${bit}bit`);
16+
bits[bit] = !bits[bit];
17+
18+
if (bitElement.style.color == "green") {
19+
bitElement.style.color = "red";
20+
}
21+
else if (bitElement.style.color == "red") {
22+
bitElement.style.color = "green";
23+
}
24+
25+
let num = 0;
26+
for (const [k, v] of Object.entries(bits)) {
27+
if (v) num += Number.parseInt(k);
28+
}
29+
counter.innerText = num.toString(2).padStart(8, '0');
30+
base10Counter.innerText = num.toString()
31+
32+
}

0 commit comments

Comments
 (0)