Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

๐ŸŒ™