JavaScript Operator: bitwise NOT
[this page | pdf | back links]
In JavaScript,
the ~ operator is
the bitwise OR operator.
For example, ~ 6 in binary notation is ~ 110 so is 1(29 times)001, which is interpreted as a negative number
so is treated as -7 in decimal
notation.
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>table,th,tr,td {border: 1px solid black;
border-collapse: collapse;}</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>
<script>
document.getElementById("Example").innerHTML =
'var x = ~ 6;';
document.getElementById("Result").innerHTML = ~ 6;
</script>
</body>
</html>
|
NAVIGATION LINKS
Contents | Prev | Next | JavaScript Operators