# Bit manipulation in JavaScript

Last Updated : 04 Oct, 2023

Bit manipulation in JavaScript refers to the manipulation and operation of individual bits within binary representations of numbers and JavaScript uses 32-bit signed integers for numeric values. It involves using bitwise operators (&, |, ^, ~, <<, >>) to manipulate individual bits of binary numbers. These operations can be used for bitwise logic operations such as AND, OR, XOR, and shifting bits.

#### Below is a list of bitwise operators:

• Bitwise AND(&): Returns true if both operands are true.
• Bitwise OR(|): Returns true even if one operand is true.
• Bitwise XOR(^): Returns true if both operands are different.
• Bitwise NOT(~): Flips the value of the operand.
• Bitwise Left Shift(<<): Shifts the bit toward the left.
• Bitwise Right Shift(>>): Shifts the bit towards the right.
• Zero Fill Right Shift(>>>): Shifts the bit towards the right but adds 0 from the left.

Below we will describe the common bit manipulation problems in detail:

## Checking Odd or Even

In this approach, we are using bitwise AND (&) to check the least significant bit of n. If it’s set (odd), “odd” is printed; otherwise, “even” is printed.

#### Syntax:

`if (num & 1) {    // num is odd} else {    // num is even}`

Example: In this example, we are using the above-explained approach.

## Javascript

 `let n = 8; ` `if` `(n & 1) { ` `    ``console.log(n + ``" is odd."``); ` `} ``else` `{ ` `    ``console.log(n + ``" is even."``); ` `};`

Output

```8 is even.
```

## Swap Two Numbers

In this approach we Swap two variables using bitwise XOR by applying XOR twice to each variable, cancelling out the effect and achieving the swap without extra storage.

#### Syntax:

`a = a ^ b;b = a ^ b;a = a ^ b;`

Example: In this example, values of a and b are swapped using XOR. The original values are exchanged without a temporary variable.

## Javascript

 `let a = 8, b = 10; ` `a = a ^ b; ` `b = a ^ b; ` `a = a ^ b; ` `console.log(``"a:"``, a, ``"b:"``, b);`

Output

```a: 10 b: 8
```

## Finding the Single Odd Occurrence

Using XOR, iterate through an array where elements appear twice except one. XOR cancels pairs, leaving the odd-occurrence element.

#### Syntax:

`let result = 0;for (let num of nums) {    result ^= num;}`

Example: In this example, XOR array elements, cancel pairs, and find an element occurring once.

## Javascript

 `let n = [5, 4, 6, 8, 3]; ` `let result = 0; ` `for` `(let num of n) { ` `    ``result ^= num; ` `} ` `console.log(``"element that occurs only once:"``, result);`

Output

```element that occurs only once: 12
```

## Power of Two Checks

In this approach, we are Checking if a number is a power of two using bitwise AND of num and (num – 1).

Syntax:

`function isPowerOfTwo(num) {    return (num & (num - 1)) === 0 && num > 0;}`

Example: In this example, we are using the above-explained approach.

## Javascript

 `function` `GFG(num) { ` `    ``if` `(num <= 0) { ` `        ``return` `false``; ` `    ``} ` `    ``while` `(num > 1) { ` `        ``if` `(num % 2 !== 0) { ` `            ``return` `false``; ` `        ``} ` `        ``num /= 2; ` `    ``} ` `    ``return` `true``; ` `} ` `console.log(GFG(16)); ` `console.log(GFG(9)); `

Output

```true
false
```

## Count Total Bits Set

In this approach, we Count the total set bits in a number using a loop with bitwise AND operation and right shift.

Syntax:

`function countSetBits(num) {    let count = 0;    while (num) {        count += num & 1;        num >>= 1;    }    return count;}`

Example: Here we are using the above-explained approach.

## Javascript

 `function` `Geek(num) { ` `    ``let count = 0; ` `    ``while` `(num > 0) { ` `        ``count += num & 1; ` `        ``num >>= 1; ` `    ``} ` `    ``return` `count; ` `} ` `console.log(Geek(11)); ` `console.log(Geek(7));`

Output

```3
3
```

Previous
Next