HTML and CSS Reference

In-Depth Information

•

>> (sign-propagating right shift)

This operator shifts the first operand the specified number of bits to the right.

Excess bits shifted off to the right are discarded. Copies of the leftmost bit are

shifted in from the left.

•

>>> (zero-fill right shift)

This operator shifts the first operand the specified number of bits to the right.

Excess bits shifted off to the right are discarded. Zero bits are shifted in from the

left. For example,
19>>>2
yields 4, because 10011 shifted two bits to the right

becomes 100, which is 4. For nonnegative numbers, zero-fill right shift and

sign-propagating right shift yield the same result.

Shift operators convert their operands to 32-bit integers and return a result of the

same type as the left operator.

EXAMPLE
5.13

<html>

<head>

<title>Bitwise Operators</title>

</head>

<body bgcolor="lightblue">

<font size="+1" face="arial">

<h3> Testing Bitwise Operators</h3>

<script type="text/javascript">

1

var result = 15 & 9;

document.write("15 & 9 yields: " + result);

2

result = 15 | 9;

document.write("<br /> 15 | 9 yields: " + result);

3

result = 15 ^ 9;

document.write("<br /> 15 ^ 9 yields: " + result);

4

result = 9 << 2;

document.write("<br /> 9 << 2 yields: " + result);

5

result = 9 >> 2;

document.write( "<br /> 9 >> 2 yields: " + result);

6

result = -9 >> 2;

document.write( "<br /> -9 >> 2 yields: " + result);

7

result = 15 >>> 2;

document.write( "<br /> 15 >>> 2 yields: " + result);

</script>

</font>

</body>

</html>

EXPLANATION

1

The binary representation of 9 is 1001, and the binary representation of 15 is

1111. When the bitwise
&
(AND) operator is applied to
1111 & 1001
, the result

is binary 1001 or decimal 9.

Continues