swap Bits Algorithm

For simplicity, main memory is named" RAM" (an acronym of" random-access memory") and secondary storage is named" disk" (a shorthand for" hard disk drive, drum memory or solid-state drive"), but the concepts do not depend on whether these terms use literally to a specific computer system. paging is an important part of virtual memory implementations in modern operating systems, use secondary storage to let programs exceed the size of available physical memory. Subsequent architectures used memory segmentation, and individual plan sections became the units exchanged between disk and RAM.Ferranti introduced paging on the Atlas, but the first mass-market memory pages were concepts in computer architecture, regardless of whether a page moved between RAM and disk. This zone of memory was named a page.
#include <iostream>
#include <bitset>

int swapBits(int n, int p, int q)
{
    if (p == q)
    {
        return n;
    }
    // If bits are same, no point swapping.
    // Determine if bits are different
    //
    if (((n & (1 << p)) >> p) ^ ((n & (1 << q)) >> q))
    {
        // toggle bits at p and q positions
        //
        n ^= (1 << p);
        n ^= (1 << q);
    }

    return n;
}

int main()
{
    int n, p, q;
    std::cout << "Enter a number: ";
    std::cin >> n;
    std::cout << "Enter bit position 1:";
    std::cin >> p;
    std::cout << "Enter bit position 2:";
    std::cin >> q;
    int r = swapBits(n, p, q);
    std::cout << "Number before swap :" << n << " (" << std::bitset<8>(n).to_string() << ") "
        << std::endl <<  "Number after swapping bits at position "
        << p << " and " << q << " : "  << r << " (" << std::bitset<8>(r) << ")"
        << std::endl;
    return 0;
}

LANGUAGE:

DARK MODE: