Horje
Swap all Odd & Even Bits using JavaScript

The task is to swap all odd and even bits in a given decimal number. This involves manipulating the binary representation of the number by isolating odd and even bits, shifting them accordingly, and then combining them to produce the result. The goal is to exchange the positions of each pair of odd and even bits to obtain a modified decimal number.

Example:
Input: 170
Output: 85
Explanation: In binary, 170 is represented as 10101010. When we swap the odd and even bits, we get 01010101, which is 85 in decimal. Each pair of odd and even bits has been exchanged, resulting in the modified decimal number.

Using Bit Manipulation

  • Initialize variables to store odd and even bits.
  • Extract odd bits by performing bitwise AND with a mask that has ‘1’ at all odd positions.
  • Extract even bits by performing bitwise AND with a mask that has ‘1’ at all even positions.
  • Right-shift odd bits to move them to even positions.
  • Left shift even bits to move them to odd positions.
  • Combine modified odd and even bits using bitwise OR operations.
  • Return the result as the modified decimal number.

Example: To demonstrate swaping all the odd and even bits using but manipulation in JavaScript.

JavaScript
function swapBits(num) {
    let oddBits = num & 0xAAAAAAAA;
    let evenBits = num & 0x55555555; 
    oddBits >>= 1;
    evenBits <<= 1;
    return oddBits | evenBits;
}
const num = 170;
console.log(swapBits(num)); 

Output
85

Time Complexity: O(1)

Auxiliary Space: O(1)

Using Lookup Table

The lookup table approach involves precomputing the swapped values for all possible 8-bit numbers and storing them in a table. This table is then used to quickly swap the odd and even bits of any given 32-bit number.

  • Create Lookup Table: Generate a lookup table where each index represents an 8-bit number, and the corresponding value represents the result of swapping its odd and even bits.
  • Split Input Number: Split the 32-bit input number into four 8-bit bytes.
  • Lookup Swapped Values: Use the lookup table to find the swapped values for each byte.
  • Combine Swapped Bytes: Combine the swapped bytes to form the final 32-bit result.

Example: To demonstrate swaping all odd and even bits using lookup table in JavaScript.

JavaScript
function createTable() {
    const table = new Array(256);

    for (let i = 0; i < 256; i++) {
        const oddBits = (i & 0xAA) >> 1;
        const evenBits = (i & 0x55) << 1;
        table[i] = oddBits | evenBits;
    }

    return table;
}

function swapBits(num, lookupTable) 
{
    const byte1 = (num >> 24) & 0xFF;
    const byte2 = (num >> 16) & 0xFF;
    const byte3 = (num >> 8) & 0xFF;
    const byte4 = num & 0xFF;

    const swappedByte1 = lookupTable[byte1];
    const swappedByte2 = lookupTable[byte2];
    const swappedByte3 = lookupTable[byte3];
    const swappedByte4 = lookupTable[byte4];

    const result = (swappedByte1 << 24)
        |
        (swappedByte2 << 16)
        |
        (swappedByte3 << 8)
        | swappedByte4;

    return result;
}

// Create the lookup table
const lookupTable = createTable();

const input = 23;
console.log(swapBits(input, lookupTable)); 

Output
43

Time Complexity: O(1)

Auxiliary Space: O(1)

Using Loop and Shift

This approach involves iterating through each pair of bits (odd and even) in the input number using a loop. Within the loop, we extract the odd and even bits, shift them to their respective positions (odd bit to even position and even bit to odd position), and then combine them to form the swapped number. Finally, we accumulate the swapped bits to get the result. It’s a simple and straightforward method for swapping odd and even bits in a number.

  • We iterate through each pair of bits (odd and even) in the input number using a loop.
  • Inside the loop, we extract the odd bit and the even bit by shifting the input number accordingly.
  • Then, we shift these bits back to their respective positions (odd bit to even position and even bit to odd position) and combine them using bitwise OR operation.
  • Finally, we accumulate the swapped bits to form the result.

Example: To demonstrate swaping all the odd and even bits using loop and shift in JavaScript.

JavaScript
function swapShift(num) {
    let result = 0;
    for (let i = 0; i < 32; i += 2) {
        const oddBit = (num >> i) & 1; 
        const evenBit = (num >> (i + 1)) & 1;
        result |= (evenBit << i) | (oddBit << (i + 1));
    }
    return result;
}
const input = 23;
console.log(swapShift(input));

Output
43

Time Complexity: O(1)

Space Complexity: O(1)




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Building a Pomodoro Timer with VueJS Building a Pomodoro Timer with VueJS
How to Compare Objects in JavaScript? How to Compare Objects in JavaScript?
JavaScript Program to Print Nth Non Fibonacci Number JavaScript Program to Print Nth Non Fibonacci Number
JavaScript Braces vs Brackets JavaScript Braces vs Brackets
Filter Array of Objects with Another Array of Objects in JavaScript Filter Array of Objects with Another Array of Objects in JavaScript

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17