Thursday, 23 August 2018

Javascript: Bitwise operators

Following table summarizes the bitwise operators supported by Javascript.

Operator
Description
~
Inverts bit pattern
<< 
Perform Left shift
>> 
Perform Right Shift
>>> 
Perform unsigned right shift
&
Bit wise AND
|
Bit wise OR
^
Bit wise Exclusive OR

Before discuss about the Bit wise operators, will discuss about Binary representations of a number

1's Complement
1's complement simply inverts the bits, if the bit is set to zero, it sets the bit to 1 and if the bit is set to 1 it sets the bit to zero.

Ex:
8 in binary            = 0 0 0 0 1 0 0 0
1's complement(8) = 1 1 1 1 0 1 1 1

2's complement
Negative numbers are stored in 2's complement form. i.e., -8 is 2's complement of 8.
2's complement is sum of (1's complement of a number + 1)

Ex:
8 in binary            = 0 0 0 0 1 0 0 0
1's complement(8) = 1 1 1 1 0 1 1 1
2's complement(8) =               (+) 1
                               -----------------
                                1 1 1 1 1 0 0 0
                               ----------------

-8 in 2's complement form is 11111000

Inversion (~)
Inversion operator change the bit value to zero, if it is one and change the bit value to one if it is zero.

Example
a = 7;

a in binary format is       0 0 0 0 0 1 1 1
~(a) in binary format is  1 1 1 1 1 0 0 0
~(a) represents -8 in binary format.       
        
Bit Wise Left Shift(<<)
Left shift operator shifts all the bits to one bit left, i.e, it doubles the value.

Syntax:
    variable << n

    left shift the variable by 'n' number of times.

    Variable << n is equals to variable = variable * 2 power n

         var1 = 2;
        var1 << 1 = 2 * 2 power 1 = 4
        var1 << 2 = 2 * 2 power 2 = 8
        var1 << 9 = 2 * 2 power 9 = 1024

Bit Wise Right Shift(>>)
Right shift operator simply shifts all the bits to one bit right, i.e, it simply half (divide by 2) the value

   Syntax:
   variable >> n
      right shift the variable by 'n' number of times.

   Variable >> n is equals to variable = variable / 2 power n

   var1 = 128;
   var1 >> 1 = 128 / (2 power 1) = 64
   var1 >> 2 = 128 / (2 power 2) = 32

Bit Wise unsigned Right Shift(>>>)
Shifts all the bits to one bit right, and make the left most bit to zero.
  
-128 in 2's complement form is

11111111 11111111 11111111 10000000

>>> shifts the one bit right and replace leftmost bit by zero

so (-128>>>1) is like below sequence

01111111 11111111 11111111 11000000 = 2147483584 (in decimal)


Bit Wise AND(&), OR(|), Exclusive OR(^)
Bit 1
Bit 2
&
|
^
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0
For a = 8 and b = 9.

a    = 0000 1000
b    = 0000 1001
a&b  = 0000 1000 (8 in decimal)

a    = 0000 1000
b    = 0000 1001
a|b  = 0000 10001 (9 in decimal)

a     = 0000 1000
b     = 0000 1001
a^b   = 0000 0001 (1 in decimal)

bitwise.html

<!DOCTYPE html>

<html>

<head>
    <title>Bitwise Operators</title>

    <style>
        p {
            font-size: 2em;
            color: seagreen;
        }
    </style>
</head>

<body>
    <p>
        <script type="text/javascript">
            var a = 7;
            var b = 2;
            var c = 128;
            var d = -128;
            var e = 8;
            var f = 9;

            document.write("a = " + a + "<br />");
            document.write("~a = " + ~a + "<br /><br />");

            document.write("b = " + b + "<br />");
            document.write("b << 1 = " + (b << 1) + "<br />");
            document.write("b << 2 = " + (b << 2) + "<br />");
            document.write("b << 9 = " + (b << 9) + "<br /><br />");

            document.write("c = " + c + "<br />");
            document.write("c >> 1 = " + (c >> 1) + "<br />");
            document.write("c >> 2 = " + (c >> 2) + "<br /><br />");

            document.write("d = " + d + "<br />");
            document.write("d >>> 1 = " + (d >>> 1) + "<br /><br />");

            document.write("e = " + e + "<br />");
            document.write("f = " + f + "<br />");
            document.write("e&f = " + (e & f) + "<br />");
            document.write("e|f = " + (e | f) + "<br />");
            document.write("e^f = " + (e ^ f) + "<br />");
        </script>
    </p>
</body>

</html>



Previous                                                 Next                                                 Home

No comments:

Post a Comment