-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbits.html
More file actions
207 lines (175 loc) · 6.73 KB
/
bits.html
File metadata and controls
207 lines (175 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bit Manipulation</title>
<link rel="stylesheet" href="bits.css">
</head>
<body>
<!-- Bit display -->
<div id="bitContainer"></div>
<!-- Mode selection -->
<div class="modes">
<label><input type="radio" name="mode" value="unsigned" checked> Unsigned</label>
<label><input type="radio" name="mode" value="signbit"> Sign Bit</label>
<label><input type="radio" name="mode" value="onescomplement"> 1's Complement</label>
<label><input type="radio" name="mode" value="twoscomplement"> 2's Complement</label>
</div>
<!-- Decimal result -->
<div id="result"></div>
<!-- Number of bits control -->
<div class="controls">
<label for="bitCount">Number of Bits:</label>
<input type="number" id="bitCount" value="16" min="1">
</div>
<!-- Show powers of two toggle -->
<div class="controls">
<label>
<input type="checkbox" id="showPowersOfTwo"> Show Powers of Two
</label>
</div>
<!-- Action buttons -->
<div class="controls">
<button id="resetBitsButton">Reset All Bits</button>
<button id="invertBitsButton">Invert All Bits</button>
</div>
<script>
const bitCountInput = document.getElementById("bitCount");
let bitArray = Array.from({ length: parseInt(bitCountInput.value, 10) }, () => 0);
const bitContainer = document.getElementById("bitContainer");
const resultContainer = document.getElementById("result");
const modes = document.getElementsByName("mode");
const showPowersOfTwoCheckbox = document.getElementById("showPowersOfTwo");
const invertBitsButton = document.getElementById("invertBitsButton");
const resetBitsButton = document.getElementById("resetBitsButton");
bitCountInput.addEventListener("input", function () {
const newBitCount = parseInt(this.value, 10);
if (newBitCount > 0) {
adjustBitArraySize(newBitCount);
updateDisplay();
}
});
showPowersOfTwoCheckbox.addEventListener("change", updateDisplay);
invertBitsButton.addEventListener("click", function () {
invertAllBits();
updateDisplay();
});
resetBitsButton.addEventListener("click", function () {
resetAllBits();
updateDisplay();
});
modes.forEach((mode) => {
mode.addEventListener("change", updateDisplay);
});
function adjustBitArraySize(newSize) {
if (newSize > bitArray.length) {
bitArray = [...bitArray, ...Array(newSize - bitArray.length).fill(0)];
} else {
bitArray = bitArray.slice(0, newSize);
}
}
function updateBit(index) {
bitArray[index] = 1 - bitArray[index];
updateDisplay();
}
function resetAllBits() {
bitArray = bitArray.map(() => 0);
}
function invertAllBits() {
bitArray = bitArray.map((bit) => (bit === 0 ? 1 : 0));
}
function calculateDecimalValue(mode) {
const binaryString = bitArray.join("");
let decimalValue = 0;
switch (mode) {
case "signbit":
const sign = binaryString.charAt(0) === "1" ? -1 : 1;
decimalValue = sign * parseInt(binaryString.substring(1), 2);
break;
case "onescomplement":
if (binaryString.charAt(0) === "1") {
const invertedBinary = binaryString
.substring(1)
.split("")
.map((b) => (b === "0" ? "1" : "0"))
.join("");
decimalValue = -parseInt(invertedBinary, 2);
} else {
decimalValue = parseInt(binaryString, 2);
}
break;
case "twoscomplement":
if (binaryString.charAt(0) === "1") {
const invertedBinary = binaryString
.substring(1)
.split("")
.map((b) => (b === "0" ? "1" : "0"))
.join("");
decimalValue = -(parseInt(invertedBinary, 2) + 1);
} else {
decimalValue = parseInt(binaryString, 2);
}
break;
default:
decimalValue = parseInt(binaryString, 2);
}
return decimalValue;
}
function getSelectedMode() {
return Array.from(modes).find((mode) => mode.checked).value;
}
function updateDisplay() {
bitContainer.innerHTML = "";
bitArray.forEach((bit, index) => {
const bitWrapper = document.createElement("div");
bitWrapper.style.position = "relative";
bitWrapper.style.display = "inline-block";
const bitElement = document.createElement("div");
bitElement.classList.add("bit", bit ? "active" : "inactive");
bitElement.textContent = bit ? "1" : "0";
bitElement.addEventListener("click", () => updateBit(index));
bitWrapper.appendChild(bitElement);
if (showPowersOfTwoCheckbox.checked) {
const powerOfTwo = document.createElement("div");
const powerValue = 2 ** (bitArray.length - 1 - index);
powerOfTwo.textContent = `2^${bitArray.length - 1 - index}`;
powerOfTwo.style.position = "absolute";
powerOfTwo.style.top = "-20px";
powerOfTwo.style.left = "50%";
powerOfTwo.style.transform = "translateX(-50%)";
powerOfTwo.style.fontSize = "12px";
powerOfTwo.style.color = "#555";
const tooltip = document.createElement("div");
tooltip.textContent = powerValue.toLocaleString();
tooltip.style.position = "absolute";
tooltip.style.top = "-60px";
tooltip.style.left = "50%";
tooltip.style.transform = "translateX(-50%)";
tooltip.style.padding = "5px";
tooltip.style.backgroundColor = "#000";
tooltip.style.color = "#fff";
tooltip.style.borderRadius = "4px";
tooltip.style.fontSize = "10px";
tooltip.style.display = "none";
tooltip.style.zIndex = "10";
powerOfTwo.addEventListener("mouseenter", () => {
tooltip.style.display = "block";
});
powerOfTwo.addEventListener("mouseleave", () => {
tooltip.style.display = "none";
});
bitWrapper.appendChild(powerOfTwo);
bitWrapper.appendChild(tooltip);
}
bitContainer.appendChild(bitWrapper);
});
const selectedMode = getSelectedMode();
const decimalValue = calculateDecimalValue(selectedMode);
resultContainer.textContent = `Decimal Value: ${decimalValue}`;
}
// Initial render
updateDisplay();
</script>
</body>
</html>