--> Sayadasite: Logical Operators

Multiple Ads

Search

Menu Bar

Logical Operators

Client side Java script Server side Java script Data types  variables Operators  Expressions  Functions  Objects  Array  Date and math related objects Document objectmodel Event handling

Logical Operators: 

Combine or manipulate boolean values.

&& (Logical AND)

|| (Logical OR)

! (Logical NOT)

Logical AND (&&):

Returns true if both operands are true.

Returns false if either operand is false.

It exhibits "short-circuiting" behavior: if the first operand is false, the second operand is not evaluated, and false is returned immediately.

Can also return the value of one of the operands if they are non-boolean (e.g., 1 && 0 returns 0).

Logical OR (||):

Returns true if at least one operand is true.

Returns false only if both operands are false.

It also exhibits "short-circuiting" behavior: if the first operand is true, the second operand is not evaluated, and true is returned immediately.

Can also return the value of one of the operands if they are non-boolean (e.g., 0 || "hello" returns "hello").

Logical NOT (!):

Inverts the boolean value of its single operand.

If the operand is true, it returns false.

If the operand is false, it returns true.

Always returns a boolean value.

Example

Logical AND

let x = 6;
let y = 3;
let z = (x < 10 && y > 1)(true)

Logical OR

let x = 6;
let y = -3;
let z = (x > 0 || y > 0); (true)

Logical NOT

let x = (5 == 8);(false)
let y = !(5 == 8);(true)

Bitwise Operators: 

Perform operations on the binary representation of numbers.

& (AND)

| (OR)

^ (XOR)

~ (NOT)

<< (Left Shift)

>> (Right Shift)

>>> (Zero-fill Right Shift)

1. Bitwise AND (&)

Returns 1 if both corresponding bits are 1, otherwise 0.

JavaScript Example

const a = 5; // Binary: 0101
const b = 3; // Binary: 0011
const result = a & b; // Binary: 0001 (Decimal: 1)
console.log(result); // Output: 1

2. Bitwise OR (|)

Returns 1 if at least one of the corresponding bits is 1, otherwise 0.

JavaScript Example

const a = 5; // Binary: 0101
const b = 3; // Binary: 0011
const result = a | b; // Binary: 0111 (Decimal: 7)
console.log(result); // Output: 7

3. Bitwise XOR (^)

Returns 1 if the corresponding bits are different, otherwise 0.

Example

const a = 5; // Binary: 0101
const b = 3; // Binary: 0011
const result = a ^ b; // Binary: 0110 (Decimal: 6)
console.log(result); // Output: 6

4. Bitwise NOT (~)

Example

Inverts all the bits of its operand. This results in the two's complement of the number minus one.

const a = 5; // Binary: 0...0101
const result = ~a; // Binary: 1...1010 (Decimal: -6)
console.log(result); // Output: -6

The 1's complement of a binary number is obtained by flipping all its bits. 0 becomes 1, and 1 becomes 0. Positive numbers remain unchanged whereas negative numbers are represented by taking the 1's complement of their positive counterparts.

For example, in 8-bit notation:

1) +9 is represented as 00001001.

     -9 is represented as 11110110, which is the 1's complement of 00001001.

2) Input: s = "0111" (7)
   Output:       1000
Explanation: Each bit is flipped, i.e. 0 becomes 1, and 1 becomes 0.

3) Input: s= "1100" (12)
   Output:      0011
Explanation: Each bit is flipped, i.e. 0 becomes 1, and 1 becomes 0.

 

The 2’s complement of a binary number is obtained by finding the 1’s complement (flipping all bits) and then adding 1 to the result. In 2’s complement representation, the Most Significant Bit (MSB) represents the sign. A 0 indicates a positive number, while a 1 indicates a negative number. The remaining bits represent the magnitude.

Positive numbers are represented the same way as in 1’s complement and sign-bit representation. Negative numbers are obtained by taking the 2’s complement of their positive counterparts.

Examples:

1) Input: s = "0111" (7)
    Output: 1001
Explanation: Find 1's complement -> 1000, then add 1 -> 1000 + 1 = 1001

2) Input: "1100" (12)
    Output: 0100
Explanation: Find 1's complement -> 0011, then add 1 -> 0011 + 1 = 0100

5. Left Shift (<<)

Shifts the bits of the first operand to the left by the number of positions specified by the second operand. Zeros are pushed in from the right.

JavaScript Example

1) const a = 5; // Binary: 0101(0101)
const result = a << 1; // Binary: 1010 (Decimal: 10)
console.log(result); // Output: 10

0

1

0

1

1

0

1

0

 

 

 

2) Original number: 64, Binary: 01000000

const result = a << 1;

After left shift: -128, Binary: 10000000

0

1

0

0

0

0

0

0

1

0

0

0

0

0

0

0

 

 

 

6. Signed Right Shift (>>)

Shifts the bits of the first operand to the right by the number of positions specified by the second operand. Copies of the leftmost bit are pushed in from the left, preserving the sign.

Example 1

const a = 5; // Binary: 0101
const result = a >> 1; // Binary: 0010 (Decimal: 2)
console.log(result); // Output: 2

0

1

0

1

0

0

1

0

 



 

Example 2
const c = 20; // Binary: 00010100
const result C = c >> 2; // Binary: 00000101

console.log(result C); // Output: 5

0

0

0

1

0

1

0

0

0

0

0

0

0

1

0

1

 

 

 

7. Unsigned Right Shift (>>>)

Shifts the bits of the first operand to the right by the number of positions specified by the second operand. Zeros are pushed in from the left, regardless of the sign.

Example 1

const a = 5; // Binary: 0...0101
const result = a >>> 1; // Binary: 0...0010 (Decimal: 2)
console.log(result); // Output: 2

0

1

0

1

0

0

1

0

 



 

Example 2
const c = 2; // Binary: 0...1111 0000
const result C = c >>> 2; // Binary: 0...00111100
console.log(result C); // Output: 60

1

1

1

1

0

0

0

0

0

0

1

1

1

1

0

0

 

 

 

Other Operators:

typeof (Returns the data type of a variable)

instanceof (Checks if an object is an instance of a specific class) Results will be true / false

?: (Ternary operator - a shorthand for if-else statements)

delete (Deletes an object's property or an element from an array)

in (Checks if a property exists in an object)

Operator Precedence and Associativity:

In JavaScript, operator precedence and associativity are fundamental concepts that dictate the order in which operations are performed within an expression. 

Operator Precedence:

Operator precedence determines which operators are evaluated first in an expression containing multiple operators. Operators with higher precedence are executed before operators with lower precedence. This is similar to the order of operations in mathematics (e.g., multiplication and division before addition and subtraction).

For instance, in the expression 3 + 5 * 2, the multiplication operator (*) has higher precedence than the addition operator (+). Therefore, 5 * 2 is evaluated first, resulting in 10, and then 3 + 10 is calculated, yielding 13.

Associativity:

Associativity comes into play when an expression contains multiple operators of the same precedence level. It determines whether the operations are performed from left-to-right or right-to-left. 

Left-to-right associativity: 

Most operators in JavaScript are left-to-right associative. For example, in 10 - 5 + 3, both subtraction and addition have the same precedence. Due to left-to-right associativity, 10 - 5 is evaluated first, resulting in 5, and then 5 + 3 is calculated, yielding 8.

Right-to-left associativity: 

Some operators, like assignment operators (=, +=, etc.), the exponentiation operator (**), and unary operators (e.g., !, typeof), are right-to-left associative. For example, in a = b = 5, the assignment happens from right to left: b = 5 is executed first, and then a = b.

Using Parentheses:

Parentheses () can be used to explicitly control the order of evaluation, overriding both precedence and associativity. Operations within parentheses are always evaluated first. For example, in (3 + 5) * 2, the addition 3 + 5 is performed first due to the parentheses, resulting in 8, and then 8 * 2 is calculated, yielding 16.

Understanding operator precedence and associativity is crucial for writing correct and predictable JavaScript code, especially when dealing with complex expressions.

No comments: