Precedence and Associativity of Bitwise Operators in TypeScript

Bitwise Operators Precedence and Associativity in TypeScript

 
Operators in TypeScript have rules of Precedence and Associativity that determine how expressions are evaluated. Here I describe Precedence and Associativity separately.
 

Precedence

 
Precedence rules determine which operators should be applied first. For operators with different precedence, the one with the highest precedence is always applied first. You will see that when an expression mixes & and | then that evaluation must be done in the correct order. In an expression, the operator with the highest precedence is grouped with an operand first, then the next highest operator will be grouped with these operands, and that process continues. The following table describes the precedence of bitwise operators in decreasing order, in other words, number 5 (Five) has less precedence than all the other operators.
 
Precedence Operator Associativity
1 ~(Bitwise negation) Right to left
2 <<(Bitwise LeftShift) , >>(Bitwise RightShift) Left to Right
3 & (Bitwise AND) Left to Right
4 ^(Bitwise XOR) Left to Right
5 | (Bitwise Or) Left to Right
 
For example, if we write this code:
  1. var a = 2,  
  2.  b = 3,  
  3.  c = 4,  
  4.  d = 2,  
  5.  e = 5;  
  6. var x = a | b & c << d ^ e;  
Then the result is that a is 7 because the preceding example is evaluated that way.
 
(a)
c<<d will be evaluated first, due to precedence and the result will be 16.
 
(b)
After Step a, the remaining part, b&16 or 3&16 will be evaluated and the result will be 0 (zero).
 
(c)
After Step b, the remaining part, 0^e or 0^5 will be evaluated and the result will be 5.
 
(d)
After Step c, the remaining part, a|5 or 2|5 will be evaluated and the result will be 7, in other words, var x=7 is the final result.
 

Associativity

 
If in any expression contains several bitwise operators of the same Precedence, then how to solve that expression? The answer of this question is Associativity, and bitwise operators, which have the same precedence in a single expression or say when two or more operators (such as (<<) and (>>)) with the same precedence can be applied to the same operand, the left  to right Associativity will cause the left-most operator to be applied first. And if two of the same operators are applied in a single statement, then left to right associativity will cause (inseated of (~) bitwise negation operator).
 
For example
, if we write this code:
  1. var a = 2,  
  2.  b = 3,  
  3.  c = 4,  
  4.  d = 3,  
  5.  e = 2,  
  6.  val;  
  7. var y = a | b | b & c << d ^ e;  
Then the result is that a is 7 because the preceding example is evaluated that way.
 
(a)
c<<d will be evaluated first, due to precedence and the result will be 16.
 
(b)
After Step a, the remaining part, b&16 or 3&16 will be evaluated and the result will be 0 (zero).
 
(c)
After Step b, the remaining part, 0^e or 0^5 will be evaluated and the result will be 5.
 
(d)
After Step c, the remaining part, a|b|5  will be evaluated from left to right, in other words first, a | b or 2 | 3 will be evaluated and the result will be 3.
 
(e)
And finally 3|b or 3|5  will be evaluated and the result will be 7, in other words, var y==7.
 
The following examples show how to do Precedence and Associativity of Bitwise operators in TypeScript. Create a program using Bitwise operators.
 
BitWise
.ts
  1. class BitWise {  
  2.  MyFunction() {  
  3.   var a = 2,  
  4.    b = 3,  
  5.    c = 4,  
  6.    d = 2,  
  7.    e = 5;  
  8.   var x = a | b & c << d ^ e; //Precedence  
  9.   document.write("Precedence result=" + x + "</br>");  
  10.   var y = a | b | b & c << d ^ e; //Associativity  
  11.   document.write("Associativity result=" + y);  
  12.  }  
  13. }  
  14. window.onload = () => {  
  15.  var call = new BitWise();  
  16.  call.MyFunction();  
  17. }  
default.html
  1. <!DOCTYPEhtml>  
  2. <htmllang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <metacharset="utf-8"/>  
  6.         <title>TypeScript HTML App</title>  
  7.         <linkrel="stylesheet"href="app.css"type="text/css"/>  
  8.         <scriptsrc="app.js">  
  9.         </script>  
  10.     </head>  
  11.     <body>  
  12.         <divid="content"/>  
  13.     </body>  
  14. </html>  
app.js
  1. var BitWise = (function() {  
  2.  function BitWise() {}  
  3.  BitWise.prototype.MyFunction = function() {  
  4.   var a = 2;  
  5.   var b = 3;  
  6.   var c = 4;  
  7.   var d = 2;  
  8.   var e = 5;  
  9.   var x = a | b & c << d ^ e;  
  10.   document.write("Precedence result=" + x + "</br>");  
  11.   var y = a | b | b & c << d ^ e;  
  12.   document.write("Associativity result=" + y);  
  13.  };  
  14.  return BitWise;  
  15. })();  
  16. window.onload = function() {  
  17.  var call = new BitWise();  
  18.  call.MyFunction();  
  19. };  
Step 4
 
Output
 
bitwise-operators-precendence-associativity-in-typescript.jpg


Similar Articles