-
Notifications
You must be signed in to change notification settings - Fork 1
/
zweierkomplement.html
124 lines (113 loc) · 3.03 KB
/
zweierkomplement.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
:root {
box-sizing: border-box;
--akzentfarbe1: #00a0a2;
--akzentfarbe2: #e2b847;
--informatikfarbe: #774075;
font-family: sans-serif;
max-width: 420px;
max-height: 420px;
}
#wrapper {
background-color: #f8f8f8;
border: solid #dcdcdc 4px;
border-radius: 20px;
padding: 10px 20px;
}
.biginput {
border-radius: 5px;
background: #fff;
border: 2px solid darkgray;
text-align: right;
font-size: 2rem;
padding: 0.35rem;
width: 8rem;
margin: 0.5rem;
}
.biginput:focus {
outline: none;
border-color: #ff4d84;
}
.grid-container {
display: grid;
grid-template-columns: 11rem 10.5rem;
align-items: center;
text-align: right;
font-size: 1.1rem;
margin: 0.5rem;
}
.number {
font-family: monospace;
font-size: 1.5rem;
text-align: right;
margin: inherit;
background: #ddd;
margin-left: 1rem;
padding: 0.5rem;
border-radius: 5px;
width: 8rem;
}
.result {
background: var(--akzentfarbe2);
}
header h1 {
font-size: 2rem;
font-weight: 800;
text-align: center;
color: var(--akzentfarbe1);
}
</style>
</head>
<body>
<div id="wrapper">
<header><h1>Zweierkomplement</h1></header>
<div class="grid-container">
<div>Die Zahl</div>
<div>
<input
type="number"
id="number"
class="biginput"
onKeyDown="if(this.value.length==4 && event.keyCode!=8) return false;"
value="-10"
/>
</div>
<div>als positive Binärzahl:</div>
<div class="number" id="bin"></div>
<div>das Komplement davon:</div>
<div class="number" id="cmp"></div>
<div>addiert man eins</div>
<div class="number">+1</div>
<div>erhält man das Zweierkomplement:</div>
<div class="number result" id="result"></div>
</div>
</div>
<script>
"use strict";
const number = document.getElementById("number");
const bin = document.getElementById("bin");
const cmp = document.getElementById("cmp");
const result = document.getElementById("result");
function dec2bin(dec) {
return (dec >>> 0).toString(2);
}
function update() {
let i = Number(number.value);
bin.innerHTML = dec2bin(-i).padStart(8, "0");
cmp.innerHTML = dec2bin(~(-i)).slice(-8);
result.innerHTML = dec2bin(i).slice(-8).padStart(8, "0");
}
number.oninput = function () {
if (number.value > 0) number.value = -1;
if (number.value < -128) number.value = -128;
update();
};
update();
</script>
</body>
</html>