Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

Nibble Swapping in C (Hexadecimal Input) in C

Nibble Swapping in C

๐Ÿ”ถ Nibble Swapping in C (Hexadecimal Input)

#include<stdio.h>
int main( )
{
    unsigned int num;
    printf("Enter the number(in hex):");
    scanf("%x",&num);
    num = num & 0xFF;
    unsigned int swapped = ((num & 0x0F) << 4) | ((num & 0xF0) >> 4);
    printf("After swapping number is: %02X\\n", swapped);
    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program swaps the upper and lower 4-bit nibbles of an 8-bit hexadecimal number.

๐Ÿ”น `scanf("%x", &num);` reads a hexadecimal value.
๐Ÿ”น `num & 0xFF` ensures the value is within 8 bits.
๐Ÿ”น `(num & 0x0F) << 4` moves the lower nibble to the upper nibble position.
๐Ÿ”น `(num & 0xF0) >> 4` moves the upper nibble to the lower nibble position.
๐Ÿ”น The final result is obtained by combining both using bitwise OR (`|`).
๐Ÿ”น `%02X` prints the swapped result in uppercase hexadecimal format with leading zero if needed.

๐Ÿ” Sample Output:

Enter the number(in hex):3C
After swapping number is: C3

Enter the number(in hex):F0
After swapping number is: 0F
    

๐Ÿท️ Keywords:

bitwise operations in C, swap nibbles C, C program hex input, 8-bit nibble swap, binary logic in C, nibble masking, hex manipulation

Comments

Popular Posts

๐ŸŒ™