{

Learn Typescript Bitwise and Assignment Operators with examples


Typescript Bitwise and Assignment comma Operators

Typescript operators: Learn bitwise, assignment, and comma operators with examples.

Typescript Bitwise operators

Bitwise operators are used to evaluating bit-based operations.

Sometimes, it is required to evaluate bits of integer values, We have to use bitwise operators.

There are several operators like in many programming languages. It works with 32 bits of an integer.

These operators work on binary operands.

Syntax:

Operand1 Operator Operand2

Operand1 and Operand2 are javascript expressions. Operators are bitwise operators.

Let’s assume that the value of A and B are 5 and 10, and the result is as follows.

OperatorTitleDescriptionExample
&Bitwise ANDEvaluates And operation on bits Integers parametersA & B is 0
|Bitwise OREvaluates Or operation on bits Integers parametersA | B is 15
^Bitwise XOREvaluates exclusive operation on bits of Integers parametersA ^ B is 15
~Bitwise NOTNot operator on bits of parameter~A is -6
«Left ShiftsFirst shifts left bits of the first operand by second operand times and the remaining bits will be filled with zero
>>Right ShiftsFirst shifts right bits of the first operand by second operand times and the remaining bits will be filled with zeroA»B is 0
Right Shifts with zeroEvaluation is right shift + left Bits shifted replaced with zeroA»>B is 0

Following is a Bitwise operators Example.

var A = 5  
var B = 10  
console.log(A & B) // returns 0  
console.log(A | B) // returns 15  
console.log(~A) // returns -6  
console.log(A^B) // returns 15  
console.log(A<**>B) // returns 0  
console.log(A>>>B) // returns 0  

Typescript Assignment Operators

Assignment operators do some operations and assign the value from left to right values.

Syntax:

Operand1 Assignment-operator Operand2

The following table explains it with examples.

OperatorTitleDescriptionExample
=Simple assignEvaluates, And operation on bits Integers parametersa=2 and value of a is 2
+=Add And assignAdd right operand to left and assign the result to left operanda+=2 and value of a is equal to a=a+2
-=Subtract and assignSubtract right operand to left and assign the result to left operanda-=2 and value of a is equal to a=a+2
*=Multiply And assignMultiply right operand with left and assign the result to left operanda*a=2 is equal to a=a*2
/=Divide and assignDivide right operand with left and assign the result to left operandc/=2 is equal to c=c/2
%=Modulus And AssignDivide right operand with left and assign the result of the remainder to left operandc%=2 is equal to c=c%2
«=Left Shifts And assignPerform left shift and result is assigned to left operandc«=2 is equal to c=c«2 td="">
>>=Right Shifts And assignPerform Right shift and result is assigned to left operandc»=2 is equal to c=c»2
&=Bitwise And AssignPerform Bitwise And and the result is assigned to left operandc&=2 is equal to c=c&2
^=Bitwise Exclusive And AssignPerform Bitwise Exclusive OR and result is assigned to left operandc^=2 is equal to c=c^2
|=Bitwise Or And AssignPerform Bitwise inclusive OR and the result is assigned to left operandc|=2 is equal to c=c|2

Typescript Comma Operator

This operator symbol is a comma(,).

This is used to perform the multiple expressions and return the last expression value.

Multiple expression is evaluated from left to right.

Syntax :-

 (Operand/Expression,Operand2)

Here is an example Example

var variable =10;  
variable = (--variable, variable);  
console.log(variable); // outputs 9  

Conclusion

To summarize, you learned about typescript operators with examples.

  • Bitwise operator
  • Comma operator
  • Assignment operator
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.