summaryrefslogtreecommitdiff
path: root/reference/C/CONCEPT/JavaSim.html
blob: 416e666d0e82d2a0352e3e88100e0e2aa5318106 (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<title>Bit Manipulation Simulator</title>

<head>
<script language="JavaScript">

function BitWise(form)
{
  // ... Convert the supplied hex numbers to decimal.
  
  var Left  = HexToDec(form.Left.value);
  var Right = HexToDec(form.Right.value);

  // ... Get the operation symbol (& | >> <<);
  
  var Operation  = form.Operation[form.Operation.options.selectedIndex].value;

  Oper = Operation;
  
  // ... Do the calculation in decimal.
  
  var DecAns = eval(Left + Operation + Right);
  
  // ... convert the answer back to Hex format.

  var BinAns = DecToBin(DecAns);        	

  form.Ans.value = BinToHex(BinAns);

  // Butify the inputs.

  BinAns = DecToBin(Left);        	

  form.Left.value = BinToHex(BinAns);

  BinAns = DecToBin(Right);        	

  form.Right.value = BinToHex(BinAns);

  // refresh the screen so the table can display the updated values.

  history.go(0);
}

function HexToDec(Hex)
{
  return(parseInt(Hex, 16));
}

function DecToBin(Decimal)
{
  var Index = 128;		               /* Just look at the 8 
						* low order bits */
  var Binary ="";

  while(Index > 0)
  {
    if (Index == 8) Binary += " ";     /*  Put a space between the  */
						 //  Bytes */

    if((Decimal % Index) != Decimal)
    {
      Binary += "1";
      Decimal -= Index;
    }
    else
    {
      Binary += "0";
    }
    Index /= 2;

    // Bodge...
    
    if (Index == 0.5) Index = 0;
  }
  return(Binary)
}

function BinToHex(Binary)
{
  var Left  = Binary.substring(0,4);
      Left  = BinToHexConv(Left);
  var Right = Binary.substring(5,9);
      Right = BinToHexConv(Right);

return(Left+Right);
}

function BinToHexConv(Binary)
{
  this["0000"] = "0";
  this["0001"] = "1";
  this["0010"] = "2";
  this["0011"] = "3";
  this["0100"] = "4";
  this["0101"] = "5";
  this["0110"] = "6";
  this["0111"] = "7";
  this["1000"] = "8";
  this["1001"] = "9";
  this["1010"] = "A";
  this["1011"] = "B";
  this["1100"] = "C";
  this["1101"] = "D";
  this["1110"] = "E";
  this["1111"] = "F";

  return(this[Binary])
}

</script>
</head>

<body bgcolor="#ffffcc">
<hr>
<center>
<h1>Bit Manipulation Simulator</h1>
</center>
<hr>

This page requires JavaScript support to test the various bitwise operators.
If you do not have JavaScript available, you can try the CGI supported
simulator. 
<p>

<hr>
<p>
<center>
<form name="form1">
Enter the Hex values and select an operator, then press the 'Execute'
button to see the results.
<p>

<input type="text"   value="00" name="Left" size=4>
<select name="Operation">
<option value=" & ">  &
<option value=" | ">  |
<option value=" << ">  <<
<option value=" >> ">  >>
</select>

<input type="text"   value="00"      name="Right" size=4>
<input type="Button" value="Execute" name="Ex"    onclick="BitWise(this.form)">
<input type="text"   value="00"      name="Ans"   size=4>
</form>
</center>

<center>
<table border=2 bgcolor="ivory">
<tr align=center>

<td>Hex</td>

<td>Binary</td>

<td>Decimal</td>
</tr>

<tr><td>
<h3>
<script>

// ... When the document is loaded, this is true
// ... and the default values are set.

if (document.form1.Operation.options.selectedIndex == -1)
{
  document.form1.Operation.options.selectedIndex = 0;
  document.form1.Left.value  = 00;
  document.form1.Right.value = 00;
  document.form1.Ans.value   = 00;
}

document.writeln("<pre>");
document.writeln("    "); 
document.writeln("    " + document.form1.Left.value + " ");
document.write  (" " + document.form1.Operation[document.form1.Operation.options.selectedIndex].value);
document.writeln(document.form1.Right.value);
document.writeln("    --");
document.writeln("    " + document.form1.Right.value);
// document.writeln(" " + document.form1.Operation[1].value);
document.writeln("</pre>");
</script>
</h3>
</td>

<td>
<h3>
<script>
document.writeln("<pre>");   
document.writeln("    ");
document.writeln("    " + DecToBin(HexToDec(document.form1.Left.value)) + " ");
document.write  (" " + document.form1.Operation[document.form1.Operation.options.selectedIndex].value);
document.writeln(DecToBin(HexToDec(document.form1.Right.value)));
document.writeln("    ---------");
document.writeln("    " + DecToBin(HexToDec(document.form1.Ans.value)));
document.writeln("</pre>");
</script>
</h3>
</td>

<td>
<h3>
<script>
document.writeln("<pre>");   
document.writeln("    ");
document.writeln("    " + HexToDec(document.form1.Left.value) + "   ");
document.write  (" " + document.form1.Operation[document.form1.Operation.options.selectedIndex].value);
document.writeln(HexToDec(document.form1.Right.value));
document.writeln("    --");
document.writeln("    " + HexToDec(document.form1.Ans.value));
document.writeln("</pre>");
</script>
</h3>
</td></tr>
</table>
</center>
<p>

<font color=red>
Sections of this page controlled by 'JavaScript' cannot be printed.
</font>
<p>
<hr>
<h2>See Also:</h2>
<img src=../../GRAPHICS/whiteball.gif>
<a href=../CONCEPT/bitwise.html>Bitwise Operators</a>.
<P>
<hr>
<p>
<center>
<table border=2 width=80% bgcolor=ivory>
<tr align=center>
<td width=25%>
<a href="../cref.html">Top</a>
</td><td width=25%>
<a href="../master_index.html">Master Index</a>
</td><td width=25%>
<a href="../SYNTAX/keywords.html">Keywords</a>
</td><td width=25%>
<a href="../FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie 
<script language="JavaScript">
<!--  //
document.write(document.lastModified);
// -->
</script>
</address>
</body